Basic operation of MongoDB

Source: Internet
Author: User
Tags findone

The basic operations of MongoDB are: Add delete update and read

Start MongoDB Shell: Command line input MONGO (Centos 7)

[[email protected] test] # MONGO MongoDB Shell version:2.6.122017-08-31t17:54:18.621+0800 for are set to 4096KB 2017-08-31t17:54:18.621+0800 [Initandlisten] * *          or  less 2017-08-31t17:54:18.621+0800 [ Initandlisten] * *          http://dochub.mongodb.org/core/readahead

To add a document:

> Db.test.insert ({"key_a":"value_a"" ninserted " : 1 }" > Db.test.insert ({"key_b":"value_b "" ninserted": 1}) 

To read a document:

Find () can query multiple qualifying documents, FindOne () queries only one,

>Db.test.find () {"_id": ObjectId ("5a12da411c97109757a27d5d"),"key_a":"value_a" }{ "_id": ObjectId ("5a12dab51c97109757a27d5e"),"Key_b":"Value_b"}
>"_id" : ObjectId ("5a12da411c97109757a27d5d" " key_a " " value_a " }
> Db.test.findOne ({"Key_b":"Value_b"}){ "_id": ObjectId ("5a12dab51c97109757a27d5e"),"Key_b":"Value_b" }> Db.test.findOne ({"_id": ObjectId ("5a12da411c97109757a27d5d")}){ "_id": ObjectId ("5a12da411c97109757a27d5d"),"key_a":"value_a"}

To update a document:

> Db.test.insert ({"Key_c":"Value_c"}) Writeresult ({"ninserted": 1 })
> Db.test.findOne ({"Key_c":"Value_c"}){ "_id": ObjectId ("5A12E0A38710E2C2442EDA15"),"Key_c":"Value_c" }
> Db.test.update (Db.test.findOne ({"Key_c":"Value_c"}), {"New_key_c":"New_value_c","Key_c":"Value_c"}) Writeresult ({"nmatched": 1,"nupserted": 0,"nmodified": 1 })
> Db.test.findOne ({"Key_c":"Value_c"}){ "_id": ObjectId ("5A12E0A38710E2C2442EDA15"), "New_key_c":"New_value_c", "Key_c":"Value_c"}

To delete a document:

> db.test.remove ({"key_c":"value_c"" nremoved " : 1 }" > Db.test.findOne ({"key_c":"value_ C"}) null

Basic operation of MongoDB

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.