MongoDB creation, query, update, delete

Source: Internet
Author: User
Tags findone

In a MongoDB database, the objects that create, query, update, and delete operations are collections.

1. See which collections are in a database, before you use the database

C:\Windows\system32>mongomongodb Shell version:3.2.TenConnecting to: Test>dbtest>Show Dbs;chengdu0. 004GBfirst0. 000GBlocal0. 000GB>  Usefirst;switched toDB First>Show Collections;blogcolrunoob>

Command explanation:

MONGO---> Connect to the database to ensure that the MongoDB database service is open before executing

DB---> View the database that is currently in use

Show DBS--view local disks on those databases

Use [DatabaseName]-to a (datebasename) database, if the database does not exist, first create the database and then use

Show Collections--view collections in the current database

2. Inserting a piece of data into the collection

>Show Collections;blogcolrunoob>Dbfirst> User ={'name':'Chengdu',...        'Sex':'M',...        ' Age': A} {"Name": "Chengdu", "Sex": "M", "Age" : A }>Db.users.Insert(User); Writeresult ({"ninserted":1 })>Show Collections;blogcolrunoobusers>

You first create a Document object user, and then insert the document user into the collection users

Command explanation:

> user = {' name ': ' Chengdu ',
... ' Sex ': ' M ',
... ' Age ': 22}
{"Name": "Chengdu", "Sex": "M", "age": +---Create a Document object user

Db.users.insert (user)--Inserts the document user into the collection users, creates the collection before inserting the Document object if the users of the collection do not exist

3. Query the Document object in the current collection, there are two methods find () and FindOne (). Find () looks at all the document objects, but displays up to 20 in the shell. FindOne () View the first document object, returning only one document object.

>  >  db.users.findOne (); {        " _id ": ObjectId (" 584eafa97629396db95535da "),        " name ":" Chengdu ",        " Sex ":" M ",          >

Query a specific Document object, with parameters

>Db.users.find ({'name':'Chengdu'}); {"_id": ObjectId ("584eafa97629396db95535da"), "name": "Chengdu", "Sex": "M", "Age": A }>Db.users.findOne ({'name':'Chengdu'}); {"_id": ObjectId ("584eafa97629396db95535da"), "name": "Chengdu", "Sex": "M", "Age": A}>

4. Update a Document object in the collection

> User ={'name':'CD',...        'Sex':'M',...        ' Age': A} {"Name": "CD", "Sex": "M", "Age" : A }>Db.users.Update({'name':'Chengdu'},User); Writeresult ({"nmatched":1, "nupserted":0, "nmodified":1 })>Db.users.find (); {"_id": ObjectId ("584eafa97629396db95535da"), "name": "CD", "Sex": "M", "Age": A }>

Update the Document object with name Chengdu in the original collection using the UPDATE statement, and name changes to CD

Add a key to a Document object

> User{"Name": "CD", "Sex": "M", "Age" : A }> User. Address= 'Shanghai'; Shanghai> User{"Name": "CD", "Sex": "M", "Age" : A, "Address": "Shanghai"}>Db.users.Update({'name':'CD'},User); Writeresult ({"nmatched":1, "nupserted":0, "nmodified":1 })>Db.users.find (); {"_id": ObjectId ("584eafa97629396db95535da"), "name": "CD", "Sex": "M", "Age": A, "Address": "Shanghai"}>

5. Delete a Document object, using the Remove () method

>User1={'name':'Xiaohong',...         'Sex':'F',...         ' Age': A,...         'Address':'Beijing',...         'QQ':'12345678'} {"Name": "Xiaohong", "Sex": "F", "Age" : A, "Address": "Beijing", "QQ": "12345678"}>Db.users.Insert(user1); Writeresult ({"ninserted":1 })>Db.users.find (); {"_id": ObjectId ("584eafa97629396db95535da"), "name": "CD", "Sex": "M", "Age": A, "Address": "Shanghai"} {"_id": ObjectId ("584eb4bd7629396db95535db"), "name": "Xiaohong", "Sex": "F", "Age": A, "Address": "Beijing", "QQ": "12345678" }>Db.users.remove ({'name':'Xiaohong'}); Writeresult ({"nremoved":1 })>Db.users.find (); {"_id": ObjectId ("584eafa97629396db95535da"), "name": "CD", "Sex": "M", "Age": A, "Address": "Shanghai"}>

You first create a Document object that has a different key from the previous document object, and then insert the document object into the collection, and a collection of MongoDB can store a document object with a different number of keys, that is, you can insert it successfully, and then use the Remove method to delete the inserted document.

MongoDB creation, query, update, delete

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.