Document operations in MongoDB (2)


1. Insert the document
 1. db.collection name.insert()
  Insert one: db.user.insert({name: "Join", age: 13, address: "beijing", isDelete: 0})
  插入多个:db.user.insert([{name:"Join",age:13,address:"beijing",isDelete:0},{name:"Join2",age:13,address:"beijing",isDelete:0},{name:"Join3",age:13,address:"beijing",isDelete:0}])

 2. db.collection name.save()
  Specify _id to modify and update: db.user.save({_id:ObjectId("234567892345678"),name:"Join",age:13,address:"beijing",isDelete:0})
  If _id is not specified, it is the same as insert, which is new: db.user.save({name: "Join4", age: 13, address: "beijing", isDelete: 0})

2. Document update
 1. Update an existing document: db.collection name.update()
  db.集合名.update(<query>,<update>,{upset:<boolean>,multi:<boolean>,writeConcern:<document>})
  query: query condition
  update operator: $set----update immediately; $inc----update after accumulative increase on the original basis
  multi: optional, the default is false, if true, only the first item will be updated
  writeConcert: optional, the level of exception thrown
  eg:db.user.update({name:"Join"},{$set:{age:21}})

 2. Replace the existing document by passing in the document, db.collection name.save()
  db.collectionname.save(document data, {writeConcern:<document>})

3. Document deletion
  db.collectionname.remove()
  db.集合名.remove(<query>,{justOne:<boolean>,writeConcern:<document>})

4. Document query
  1. Query all documents in the collection: db.collection name.find()
  2. Query the documents that meet the requirements: db.collection name.find(<query>,{<key>:1,<key>:2...}), query is the requirement for judging the query, and key is the query displayed What content 1 is displayed
    eg:db.user.find({age:21},{age:1,name:1})
  3. Display in a formatted way: pretty()
    eg:db.user.find().pretty()
  4. After the query, display the first content that matches the result: findOne()
  5. Query condition operator
     
content conform to expression
more than the $gt db.collectionname.({<key>:{$gt:<value>}})
greater or equal to $gte db.collectionname.({<key>:{$gte:<value>}})
less than $lt db.collectionname.({<key>:{$lt:<value>}})
less than or equal to $lte db.collectionname.({<key>:{$lte:<value>}})
equal : db.collectionname.({<key>:<value>})
not equal to $ne db.collectionname.({<key>:{$ne:<value>}})
query using _id _id db.collection name.({"_id":ObjectId("id value")})
Number of matching results count db.集合名.({<key>:<value>}).count()
Whether a field in the query result contains a certain value /contained value/ db.collectionname.({name:/io/})
Whether a field in the query result starts with a value /^ contains the value/ db.collection name.({name:/^J/})
  6. Conditional query and, or
    and:db.集合名.find({<key>:<value>,<key2>:<value2>,<key3>:<value3>})
    or  :db.集合名.find({$or:[{<key>:<value>},{<key2>:<value2>}]})
  7、limit、skip  
    limit(): Read the specified amount of data content
      eg: db.collection name.find().limit(5) Read the first 5 pieces of data
    skip(): skip the specified amount of data content
      eg: db.collection name.find().skip(5) skip the first 5 data
  8. Sort
    db.setname.find().sort({key:1})
    1 for ascending order, -1 for descending order 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325291123&siteId=291194637