当前位置:网站首页>Mongo database
Mongo database
2022-06-28 10:36:00 【0&1 * 1】
MongoDB brief introduction
1) Open source database system based on distributed file storage .
2) For the purpose of WEB Applications provide scalable, high-performance data storage solutions
3) Store data as a document , Document similar to Json Format ,
{
name:" Xiao Ming ",
age:16,
address: {city:" Changsha ", country:"china"}
}
5、 ... and 、 data type
1) type
[ Failed to transfer the external chain picture (img-YMHwLxQo-1566960480633)(assets/1564387186297.png)]
2)MongoDB Entry and exit
[ Failed to transfer the external chain picture (img-jNui0P6B-1566960480633)(assets/1564183945811.png)]
Get into mongo sign out exit
6、 ... and 、 The document operation
1) Add the document ( data )
db. Collection name .insert(document Every piece of data , It's just one. document, It's just one. json
db.student.insert({name:'xiaoming', age:18})
When adding a document , If you don't specify _id Parameters
MongoDB The document is assigned a unique ObjectId
example : db.student.insert({'_id':1, name:'xiaoming', age:18})
Add multiple documents
db.student.insert([
{name:xiaoming', sex:' male ', age:16},
{name:’xiaobai', sex:' male ', age:18},
{name:’moran‘, sex:’ Woman ', age:18},
])
2) Query the document ( data
db. Collection name .find([conditions]) MongoDB The conditional statements of are also very powerful , The following content may cause discomfort , Please prepare yourself for
1. View all data in the collection : db.student.find()
2. Format display : db.student.find().pretty()
3. View the data that meets the conditions : db.student.find({name:'xiaoming'})
3) Nightmare conditions
| The operator | describe |
|---|---|
| $ne | It's not equal to |
| $gt | Greater than |
| $lt | Less than |
| $gte | Greater than or equal to |
| $lte | Less than or equal to |
Example :db.user.find({‘age’: {’$ne’: 45}})
1.and Conditions
{$and:[{expression1}, {expression1}, ...] }
2.or Conditions
{$or:[{expression1}, {expression1}, ...] }
3.and and or A mixture of
db.table.find({$or:[{$and:[{sex:' Woman '}, {age:18}]},{$and:[{sex:' male '}, {age:{$gt:18}}]}]})
4) Modify the document ( data )
1.db. Collection name
.update(<query>, <update>, {multi:<boolean>}
2. Modify a piece of data
db.table.update({sex:' male '}, {age:20})
3. Specify property modification
{ $set: {age:20} }
db.table.update({name:'xiaoming'}, {$set: {age:666, sex: 'xx'}} )
4. Update all documents in the collection that meet the conditions
{ multi: true }
db.table.update({sex:' male '}, {$set:{sex:' Woman '}}, { multi:true} )
5) Delete the document ( data )
1.db. Collection name
.remove(<query>, {justOne:<boolean>})
2. Delete all documents in the collection
db.table.remove({})
3. Delete all documents that meet the conditions in the collection
db.table.remove({sex: ' male '})
4. Delete only the first document in the set that meets the conditions
{ justOne: true }
db.table.remove({sex:' male '}, { justOne:true} )
7、 ... and 、Python And MogoDB Interaction
1) install
pip install pymongo
2) Establishing a connection
client = pymongo.MongoClient()
3) Specify database
db = client[ Database name ]
4) Specify the collection
collection=db[ Collection name ]
5) Basic use
1. Search for documents
find()
2. Add the document
insert()
3. Modify the document
update()
4. Delete the document
remove()
6) The main points of
1. lookup
Find a document : find_one()
Find all :find()
2. add to
Add a document :insert_one
Add more than one :insert_many()
3. Delete
Delete a document :delete_one
Delete multiple :delete_many()
4. modify
Modify a document : update_one
Modify multiple :update_many()
6) take Mongodb Decoration into class
import pymongo
# 1. Establishing a connection
client = pymongo.MongoClient()
# 2. Specify database
db = client["py_49"]
# 3. Specify the collection
col = db["student"]
# 1. Add the document
# col.insert_one({"name": "lee", "age": 18, "sex": "M"})
# col.insert_many([
# {"name": "tomas", "age": 15, "sex": "M"},
# {"name": "misa", "age": 18, "sex": "F"},
# {"name": "meng", "age": 15, "sex": "F"},
# {"name": "meixi", "age": 16, "sex": "M"},
# {"name": "que", "age": 20, "sex": "M"},
# ])
# 2. Search for documents
# res = col.find_one()
# print(res)
# res = col.find()
# print(res) # <pymongo.cursor.Cursor object at 0x7f1213e26668>
# for i in res:
# print(i)
# 3. Modify the document
# col.update_one({"name": "misa"}, {"$set": {"age": 15}})
# col.update_many({"age": 15}, {"$set": {"age": 88}})
# 4. Delete the document
# col.delete_one({"name": "misa"})
# col.delete_many({"age": 88})
col.delete_many({"$or": [{'name': 'lee'}, {'name': 'meixi'}, {'name': 'que'}]})
res = col.find()
for i in res:
print(i)
边栏推荐
- Summary of characteristics of five wireless transmission protocols of Internet of things
- MarkDown——基本使用语法
- 学习机器学习的最佳路径是什么
- Django数据库操作以及问题解决
- [unity][ecs] learning notes (II)
- 增强 Jupyter Notebook 的功能,这里有四个妙招
- How to use dataant to monitor Apache apisex
- Several methods of using ABAP to operate Excel
- Install using snap in opencloudos NET 6
- 2D code generator for openharmony application development
猜你喜欢
随机推荐
Chapter 5 trees and binary trees
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
Ribbon核心源码解析
手把手教你处理 JS 逆向之 SVG 映射
[Unity][ECS]学习笔记(一)
一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!
【NLP】今年高考英语AI得分134,复旦武大校友这项研究有点意思
Katalon框架测试一个web页面操作实例代码
As shown in the figure, the SQL row is used to convert the original table of Figure 1. Figure 2 wants to convert it
AQS understanding
sentinel
Fabric.js 笔刷到底怎么用?
appliedzkp zkevm(9)中的Bytecode Proof
[Unity][ECS]学习笔记(二)
Idea failed to connect to SQL Sever
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
一文读懂 12种卷积方法(含1x1卷积、转置卷积和深度可分离卷积等)
MySQL common commands for viewing database performance
Hystrix deployment
[unity][ecs] learning notes (II)
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)








