mongodb: key-value pair form, suitable for reptiles, Baidu maps; document - similar to the dictionary, composed by the document collection.
distributed:
- reliability
- Scalability
- Resource Sharing
- Faster
command
-
Enter: mongo
~$ mongo MongoDB shell version v3.6.3 …… 2020-02-22T16:04:39.509+0800 ICONTROL [initandlisten]
Library operations
-
Show all databases: show dbs
> show dbs admin 0.000GB config 0.000GB local 0.000GB
-
Use database: use databases, there is used, if any, to create, if not a collection of the database, the database does not display the dbs
> use python switched to db python
-
View current database: db
> db python
-
Delete the current database: db.dropDatabase () is case-sensitive, hump logo
Set operations
-
Create Collection: db.createCollection ( 'name')
> db.createCollection('stu') { "ok" : 1 }
-
Displays the current database of all collections: show collections
> show collections stu
-
To delete a collection:. Db collection name .drop ()
Document Actions
increase
-
Inserted into the collection document:. Db name set .insert ({ 'key': value, 'key': value}) _id unique identification is automatically added, may be added manually { '_id': 123}
> db.stu.insert({'_id':123,'name':'zhen','age':21}) WriteResult({ "nInserted" : 1 })
-
Inserting rows of data:. Db name set .insert ([{}, {}, {}])
> db.stu.insert([{'name':'lili','age':11},{'name':'ilil','age':21}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
check
-
Query contents of the collection: db collection name .find ().
> db.stu.find() { "_id" : ObjectId("5e53c343717845eaf7f530cc"), "name" : "li", "age" : 22 }
-
Beautify display the query contents of the collection: db collection name .find () pretty ()..
> db.stu.find().pretty() { "_id" : ObjectId("5e53c343717845eaf7f530cc"), "name" : "li", "age" : 22 } { "_id" : 123, "name" : "zhen", "age" : 21 } { "_id" : ObjectId("5e53c45e717845eaf7f530cd"), "name" : "lili", "age" : 11 }
-
Satisfy the query criteria:. Db name set .find ({ 'key': value}) key can not be quoted
> db.stu.find({name:'li'})
-
Operators
不等于:$ne 大于:$gt 小于:$lt 大于等于:$gte 小于等于:$lte db.集合名字.find({key:{$gt:20}})
-
And $ query: db name set .find. ({$ And: [{key: value}, {}]})
-
$ Or query: db name set .find. ({$ Or: [{key: value}, {}]})
db.stu.find({$or:[{name:'li'},{age:{$gt:11}}]})
-
$or 里嵌套and 查询:db.集合名字.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: or:[{and:[{key:value},{key:value}]},{$and:[{key:value},{key:value}]}]})
change
-
Modify: db name .Update set (condition modified {}, {} modified value), only a plurality of the condition of a modification; belongs to cover all modifications, add the parameter $ set to cover only the non-modified
覆盖:db.stu.update({age:22},{name:'xxx'}) 修改:db.stu.update({age:22},{$set:{name:'xxx'}}) 全部修改:db.stu.update({age:22},{$set:{name:'xxx'}},{multi:true})
delete
-
Delete the document: db collection .remove ({key: value}). Do not give conditions, only {} will delete all, delete only the first parameter qualifying: justOne: true
只删除一条:db.stu.remove({age:22},{justOne:true})
pycharm operation mongo
-
Import library
import pymongo
-
Establish a connection --pymongo.MongoClient ()
clinent = pymongo.MongoClient()
-
Connect to the database --clinent [ 'python37']
clinent['python37']
-
Connection sets --db [ 'student']
my_collection = db['student']
-
Set operations - CRUD
增一条——insert_one() my_collection.insert_one({'name':'li','age':13,'sex':'f'}) 增多条——insert_many() my_collection.insert_many([ {}, {}, {} ]) 查询——find_one() res = my_collection.find_one() # 查询一条 res = my_collection.find() #查询全部,返回的是可迭代的结果,也可以list(res)强转为列表 修改——update_one() my_collection.update_one({'name':'value'}, {'$set':{'age':20}}) 参数要用字符串的形式传递 删除——delete_one() 注意不是remove res = my_collection.delete_one({'name':'value'})