mongodb operation collection

1 mongodb create database
  shell>use mon
2 insert
  shell> db.mon.insert({"name":"ppo","age":10})
3 delete all documents
  shell> db.mon.drop();
4 delete Specify record (parameter is queryer)
  shell> db.mon.drop({_id:2})
5 Update record (parameter is queryer modifier)
  ​​shell> db.mon.update({_id:2},{$set :{name:"009"}})
6 If the update record exists in the domain, it will be updated, if it does not exist, it will be appended. If the sex domain exists, it will be updated to 1, and if it does not exist, it will be appended to 1
  shell> db.mon.update({_id:2} ,{$set:{sex:1}},true)
7 Update record batch update parameter list is queryer, updater, if it exists, the update does not exist, then append, whether to batch update
  shell>db.mon.update({_id: 2},{$set:{sex:1}},false,true)
The accumulation of 8 fields will accumulate the age with id 2 by 10
  shell>db.mon.update({_id:2},{$inc:{age :10}}})
9 If the existing domain is deleted, the age domain in the document whose id is 2 will be deleted
  shell>db.mon.update({_id:2},{$unset:{age:10}})
10 mongodb insert array
  shell>db.mon.update({_id:2},{$push:{books:"javascript"}})
  batch push mongodb array
  shell>db.mon.update({_id:2},{$pushAll :{books:["c","java","c++"]}})
11 Insert the duplicate value addToSet, if the specified books already exist, it will not be inserted repeatedly, only if it does not exist, similar to the set set
  shell >db.mon.update({_id:2},{$addToSet:{books:"java"}})
12 Set deletion operation (pop, 1 deletes the last element, -1 deletes the first element)
  shell> db.mon.update({_id:2},{$pop:{books:-1}})
13 Delete the data specified in the collection
  shell>db.mon.update({_id:2},{$push:{ books:"js"}})
  batch delete
  shell>db.mon.update({_id:2},{$pushAll:{books:["js,"java"]}})
14 addToSet and each combine batch array update
  shell>db.mon.update({_id:2},{$addToSet:{books:{$each:["js","java"]}}})

15 Complex Query
   1 Query students between the ages of 25-27, list their names and ages
   shell>db.mon.find({age:{$gte:25,$lte:27}},{_id:0,name:1,age:1})

   2 Query all students not named Xiaoming
   shell>db. mon.find({name:{$ne:"Xiao Ming"}},{_id:0,name:1,age:1})

   3 Query data that is not in the range $in no example
   shell>db.mon.find({ age:{$nin:["10","30"]}})

   4 Query students with English scores greater than 85 or Chinese scores greater than 80
   shell>db.mon.find({$or:[{yingyu:{$gt :85}},{yuwen:{$gt:80}}]},{_id:0,name:1,age:1})

   5 Regular query to find out the user
   shell>db.mon.find({ name:/fuck/i})

   6 Use all to query
   shell>db.mon.find({books:{$all:["js","java"]}})

   7 Array index subscript query
   shell>db.mon .find({"books.1":"java"})

   8 solve order/and query
   shell>db.mon.find({"school.score":"A","school.name":"BB"},{_id:0})

   9 Single condition group query $elemMatch
   shell>db.mon.find({school:{$elemMatch:{school:"K",score:"A"}}})

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676446&siteId=291194637