MongoDB Update and delete documents (document)

Source: Internet
Author: User
Tags mongodb update

Organize from

Https://www.shiyanlou.com/courses/running/57

Start

$ sudo service mongodb start

Enter the MongoDB command-line interface and exit at the command line by knocking exit

$ mongo

The ture of the Boolean type in the experiment is replaced with 1, false with 0 instead

Create a database

Example: After entering the MONGODB command line operator interface, do the following:

> use Chenshiswitched to db Chenshi

You can use the DB command to view your currently selected database

> dbChenshi

You can also view all databases by using the show DBS command

> show dbs

The Chenshi you just created after this command is not displayed, only after you have inserted data into the database

Delete Database-db.dropdatabase ()

Suppose we were using the statement Db.computer.insert ({"Name": "Shiyanlou"}) to insert the data into the database Chenshi

> show dbs      #显示当前系统中的所有数据库> use Chenshi   #转到Chenshi数据库switched to db Chenshi> db.dropDatabase(){"dropped":"Chenshi","ok":1}> show dbs

Create Collection-createcollection ()

Grammar:

db.createCollection(name,options)

Parameter description:

    • Name: The collection name created
    • Options: is a document that is initialized (optional)

Example:

> db.createCollection("shiyanlou")            #无参数{ "ok" : 1 }> show collectionsshiyanlousystem.indexes> db.createCollection("shiyanlou2", { capped : 1, autoIndexID : 1, size : 6142800, max : 10000 } ) #带参数{ "ok ": 1 }

Parameter description:

    • Capped: The type is Boolean, and if ture creates a fixed-size collection, the previous entry can be overwritten automatically when its entry reaches its maximum. The parameter size is also specified when setting it to ture;
    • AUTOINDEXID: The type is Boolean, the default is False, and if set to Ture, the index is automatically created on the _id Field.s;
    • Size: If capped is required for ture, specify the maximum value of the parameter, in bytes;
    • Max: Specifies the maximum number of documents. You can also not create a collection in Mogodb because the collection is created automatically when the document is created
Delete Collection-db. Collection.drop ()

Operation Example:

> use Chenshiswitched to db Chenshi> show collectionsshiyanloushiyanlou2system.indexes> db.shiyanlou.drop()ture> show collectionsshiyanlou2system.indexes

Delete Succeeded

Note: When you want to delete the specified collection, replace collection with the name of the collection you want to delete

 
Insert Document-DB. Collection_name.insert (document)

Operation Example:

> userdoc1= ({"USER_ID":1,"Name":"Cloud","State":"Active","Actor":"User","E-mail":The test@qq. com ","Vm_num":2,"Time": [{"Date":"2014-08-12","Hour":"10:53 PM"}]}) > userdoc2= ({"USER_ID":2,"Name":"Testadmin","State":"Active","Actor":"Admin","E-mail":The test@qq. com ", "Vm_num": 2, "time": [{" date ": " 2014-08-11 ", " hour ":  "06:34 AM"}] }) > doc1= ({ "name":  "Peter", " Teacher "})  #先定义文档 > use chenshiswitched to DB chenshi> Db.shiyanlou.insert (userdoc1) writeresult ({ "ninserted": 1}) > Db.shiyanlou.insert (USERDOC2) Writeresult ({ Span class= "hljs-string" > "ninserted": 1}) > Db.shiyanlou.insert (Doc1) Writeresult ({ Span class= "hljs-string" > "ninserted": 1})       

Insert a document successfully, or you can directly replace the document's content as a function parameter directly

Update document-DB. Collection_name.update (Selection_criteria,updated_data)

Operation Example:

> db.shiyanlou.update({"user_id":"02","e-mail":"test@qq.com"},{$set:{"e-mail":"group@qq.com"}})WriteResult({"nMatched":1,"nUpserted":1,"nModified":1})> db.shiyanlou.find()
    • Change the e-mail of the user_id=2 document to [email protected]
    • The first curly brace contents indicate the lookup condition, and the second curly brace content indicates the updated data
    • The default update function updates only one document, and if you want to work on all documents, you need to join Multi:ture

Operation Example:

db.shiyanlou.update({"e-mail":"test@qq.com"},{$set:{"e-mail":"group@qq.com"}},{multi:ture})
 
Replace the existing document-DB. Collection_name.save ({_id:objectid (), new_data})

Operation Example:

> db.shiyanlou.save({"_id":ObjectId("53ea174ccb4c62646d9544f4"),"name":"Bob","position":"techer"})WriteResult({"nMatched":1,"nUpserted":1,"nModified":1})

Same as update, but update is more useful



Delete Document-DB. Collection_name.remove (Delection_criteria)

Operation Example:

> db.shiyanlou.remove({"name":"Bob"})WriteResult({"nRemoved":1})

In fact, the parameter of the Remove function is the same as the first parameter of the update function, which is equivalent to the search condition, note, do not delete it accidentally!

After deletion, you can confirm the data with the Find command:

> db.shiyanlou.find()


 

MongoDB Update and delete documents (document)

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.