Based on the use of learning [database] mongodb interfaces with python

mongodb: key-value pair form, suitable for reptiles, Baidu maps; document - similar to the dictionary, composed by the document collection.
distributed:

  1. reliability
  2. Scalability
  3. Resource Sharing
  4. Faster

command

  1. Enter: mongo

     ~$ mongo
     MongoDB shell version v3.6.3
     ……
     2020-02-22T16:04:39.509+0800 ICONTROL  [initandlisten] 
    

Library operations

  1. Show all databases: show dbs

     > show dbs
     admin     0.000GB
     config    0.000GB
     local     0.000GB
    
  2. 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
    
  3. View current database: db

     > db
     python
    
  4. Delete the current database: db.dropDatabase () is case-sensitive, hump logo

Set operations

  1. Create Collection: db.createCollection ( 'name')

     > db.createCollection('stu')
     { "ok" : 1 }
    
  2. Displays the current database of all collections: show collections

     > show collections
     stu
    
  3. To delete a collection:. Db collection name .drop ()

Document Actions

increase
  1. 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 })
    
  2. 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
  1. Query contents of the collection: db collection name .find ().

    > db.stu.find()
    { "_id" : ObjectId("5e53c343717845eaf7f530cc"), "name" : "li", "age" : 22 }
    
  2. 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
    }
    
  3. Satisfy the query criteria:. Db name set .find ({ 'key': value}) key can not be quoted

    > db.stu.find({name:'li'})
    
  4. Operators

    不等于:$ne
    大于:$gt
    小于:$lt
    大于等于:$gte
    小于等于:$lte
    db.集合名字.find({key:{$gt:20}})
    
  5. And $ query: db name set .find. ({$ And: [{key: value}, {}]})

  6. $ Or query: db name set .find. ({$ Or: [{key: value}, {}]})

    db.stu.find({$or:[{name:'li'},{age:{$gt:11}}]})
    
  7. $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
  1. 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
  1. 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

  1. Import library

     import pymongo
    
  2. Establish a connection --pymongo.MongoClient ()

     clinent = pymongo.MongoClient()
    
  3. Connect to the database --clinent [ 'python37']

     clinent['python37']
    
  4. Connection sets --db [ 'student']

     my_collection = db['student']
    
  5. 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'})
    
Released five original articles · won praise 0 · Views 163

Guess you like

Origin blog.csdn.net/weixin_41655783/article/details/104484445