"MongoDB" additions and deletions to check the basic operation

Source: Internet
Author: User

View all databases

Show DBS

Switch Database (automatically created if not present)

Use DatabaseName

Delete current Database

Db.dropdatabase ()

There are no tables in MongoDB, only collections.

Insert Collection

Db. Collection name. Insert (JSON)

For example Db.connection.insert ({x:1})

For loop insertion (JS syntax)

for (i=3;i<100;i++) DB. Name of the function. Insert ({x:i})

View all Collections

Show collections

View All data

Db. Collection name. Find ()

View data that meets your criteria

Db. Collection name. Find ({x:1})

Viewing the number of data bars

Db. Collection name. Find (). Count ()

Jump over N bar data

Db. Collection name. Find (). Skip (3)

View Top N Data

Db. Collection name. Find (). Limit (2)

Sort

Db. Collection name. Find () sort ({key:1}) sorted by the value of key, 1 is ascending, 1 is descending

Full update (Old data is updated by new data)

Find data x 1 and update to x 9

Db. Collection name. Update ({x:1},{x:9})

Partial Update (there is a field that is updated and no fields remain intact)

Find to X=1,y=3; update x to 3,y remains unchanged

Db. Collection name. Update ({x:1,y:3},{$set: {x:3}})

Update + Create (If the lookup data does not exist, it is created automatically)

Db. Collection name. Update ({X:1},{x:2},true}

more than one update (If you find more than one qualifying data, all updates, be sure to use the $set identifier)

Db. Collection name. Update ({x:1},{$set: {x:2}},false,true}

Delete

Db. Collection name. Remove ({x:1})

Delete Collection

Db. Collection name. Drop ()

"MongoDB" additions and deletions to check the basic operation

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.