MongoDB Learning 2---Common command parsing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.