1,mongodb the same points and differences between insert () and save ()
Difference: If there is a primary key in the new data, insert () will prompt the error, and save () changes the original content to the new content.
Insert ({_id:1, "name": "N2"}) will report a duplicate error prompt for the primary key.
Save ({_id:1, "name": "N2"}) will change N1 to N2.
The same point: if there is no primary key in the new data, a record is added.
2,update command
UPDATE command format:
Db.collection.update (Criteria,objnew,upsert,multi)
Parameter description:
Criteria: Query criteria
Objnew:update objects and some update operators
Upsert: If there is no record of update, whether to insert objnew this new document, true to insert, default to False, not to insert.
Multi: The default is false, updating only the first record found. If true, all records that are queried by criteria are updated.
Example:
Db.classes.update ({"Count": {$gt: 20}},{$set: {"name": "C4"}},false,true)
2.1 Data Update operator
(1) $inc
Usage: {$inc: {Field:value}}
Function: Adds value to a field of a number
Example: Increase the age of the student with name Chenzhou by 5
(2) $set
Usage: {$set: {Field:value}}
Function: Sets the value of a field in a document
Db.students.update ({name: "Chenzhou"},{$set: {age:23}})
(3) $unset
Usage: {$unset: {field:1}}
Role: Delete a field
(4) $push
Usage: {$push: {Field:value}}
Function: Append value to field. Note: field can only be an array type, and if field does not exist, an array type is automatically inserted
Db.students.update ({name: "Chenzhou"},{$push: {"Ailas": "Michael"}})
The remaining additional data update operators:
http://chenzhou123520.iteye.com/blog/1637629
1 Query criteria
(1) $lt, $lte, $gt, $gte, $ne
Db.user.find ({username:{$ne: ' Zyb '}})
(2) or query
Db.user.find ({$or: [{uid:725},{name: ' Zyb '}]})
2,DISTINCT usage
3,group usage
Db.sendclick.group ({key:{idfa:1},cond:{cd:20160601,id: "xxxx"},reduce:function(obj,prev) {prev.cnum++}, Initial:{cnum:0}}). ForEach ( function(x) { if(X.cnum > 3) { Print (X.IDFA); Print (x.cnum); } })
Insert a new table after 4,find
Db.active.find ({cd:{$lt: 20160602}}). forEach(function(x) { Db.temp_ Active20160602idfa.insert ({_id:x.idfa,cid:x.cid,cd:x.cd}) })
MongoDB Learning 2---Common command parsing