MongoDB Note 1 (Basic operations, additions and deletions)

Source: Internet
Author: User
Tags bulk insert

    1. Querying an existing database

>show DBS


2. Create a database, you need to create a collection this Coucay creates

>use MyDB


3. View Collections

>show Collections or >show tables


4. Create a document and insert data

>db.userinfo.insert ({_id:1,name: "Xiaoming"})


5. Bulk Insert document, Shell is not support bulk INSERT, want to complete bulk insert can be used in MongoDB application driver or shell for loop

>for (var i =0;i<10;i++) {.. Db.userInfo.insert ({name:i}):}


6. View the contents of the document

>db.userinfo.find ()


7.save operation, the save operation and the insert operation differ in the case of encountering _id in the same situation

Save operation completed

Insert will error

> Db.userInfo.insert ({_id:1,name: "Xiaoming"}) Writeresult ({"ninserted": 1}) > Db.userInfo.insert ({_id:1,name: "Xiaocang"}) Writeresult ({"ninserted": 0, "Writeerror": {"code": 11000, "errmsg": "Insertdocument:: Caused by:: 11000 E 11000 duplicate key error index:foobar.userinfo.$_id_ dup key: {: 1.0} "}}" > Db.userInfo.save ({_id:1,name: "Xiaocang" }) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})


8. Delete all the data in the list, the collection and index will not be deleted

> Db.userInfo.remove ({}) Writeresult ({"nremoved": 1}) > Db.userInfo.find () > Show Collectionssystem.indexesuserinfo


9. Delete by condition

Db. [Documentname].remove ({})

Delete a record in the collection with name equal to Xiaoming

Db.userInfo.remove ({name: "Xiaoming"})




This article is from a "little progress every Day" blog, make sure to keep this source http://08jw3.blog.51cto.com/998816/1675975

MongoDB Note 1 (Basic operations, 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.