MongoDB additions and deletions to search and change

Source: Internet
Author: User
Tags modifiers

MongoDB comes with a JavaScript Shell, so it is possible to use the JS syntax in it.

Insert operation:

Single Insert

var single={"name":"mei"," Age ":}db.user.insert (single);

Loop insert

var single={"name":"tinyphp","num  ":",} for(var i=0;i<5; i++) { Single.num=i;db.user.insert (single);}

Find query operation:

Commonly used for:

>=, <, <=,! =, =

> Corresponding $GT

>= corresponding $gte

< corresponding $LT

<= corresponding $lte

! = corresponds to $ne

= = Direct write value such as Db.user.find ({"Age": 22})

/* Find Age >22 */ Db.user.find ({"Age":$})

&&

/* find Name= "tinyphp" && num=3 */ Db.user.find ({"name":"tinyphp"," Num ":3})

Or

/* find Name= "tinyphp" | | num=3 */ Db.user.find ({$or: [{"name":"tinyphp"},{"  num":3}]})

Inch

/* find name in ["Tinyphp", "Jack"] */ Db.user.find ({"name": {$in: ["tinyphp" ,"Jack"]})

Not in

/* find name not in ["tinyphp", "Jack"] */ Db.user.find ({"name": {$nin: ["tinyphp","  Jack"]})

Regular match

/* the Find name starts with the J */ Db.user.find ({"name":/^j/})

$where Query

Db.user.find ({$where: function () {returnthis. name=='jack'} })

Update operation:

Overall update

var model=db.user.findone ({"name":"Jack"}) Model.age=db.user.update ({"name":"  Jack"},model)

Local updates

$inc modifiers

/**/db.user.update ({"name":"Bing "},{$inc: {"Age":+})

$set modifiers

/**  /db.user.update ({"name":"Bing "},{$set: {"Age":Ten}})

True parameter for update

If the last parameter of update is true then the modification condition does not exist, automatically adding one such as:

Db.user.update ({"name":"Mark"},{$set: {"  Age":Ten}},true)

will automatically add a record: Name Mark,age is 10

If true is added and the condition is met, it will be modified in bulk, or only the first one will be updated by default.

Remove Operation:

Db.person.remove ({"name":"Jack"})

MongoDB additions and deletions

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.