MongoDB database update and deletion documentation

Source: Internet
Author: User

MongoDB database update and deletion documentation
Create a collection createCollection ()

<Span style = "font-size: 14px;"> name: the name of the created set. options: an initialized document (optional) </span>

<Span style = "font-size: 14px;"> db. createCollection ("jingdong") # No parameter {"OK": 1}> show collectionsjingdongsystem. indexes> db. createCollection ("jingdong", {capped: 1, autoIndexID: 1, size: 6142800, max: 10000}) # With parameter {"OK": 1} </span>

Capped: the type is Boolean. If it is true, a set of fixed sizes is created. When the number of entries reaches the maximum, the previous entries are automatically overwritten. Specify the parameter size when setting it to true;
AutoIndexID: Boolean type. The default value is false. If it is set to true, an index is automatically created on _ id field. s;
Size: If capped is true, specify the maximum value of the parameter, in bytes;
Max: specifies the maximum number of documents. You do not need to create a collection in mogodb, because the collection is automatically created when you create a document.

Delete COLLECTION db. COLLECTION. drop ()
<span style="font-size:14px;">> use JDswitched to db JD> show collectionsjingdongjingdong1system.indexes> db.jingdong.drop()ture> show collectionsjingdong1system.indexes</span>

Insert document db. COLLECTION_NAME.insert (document)
</Pre> <pre name = "code" class = "python"> <span style = "font-size: 14px;"> userdoc1 = ({"user_id ": 1, "name": "cloud", "state": "active", "actor": "user", "e-mail": "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": "test@qq.com", "VM_num": 2, "time": [{"date": "2014-08-11", "hour": "06:34 AM"}]})> doc = ({"name": "peter ", "position": "teacher"}) # first define the document> use JDswitched to db JD> db. jingdong. insert (userdoc1) WriteResult ({"nInserted": 1})> db. jingdong. insert (userdoc2) WriteResult ({"nInserted": 1})> db. jingdong. insert (doc1) WriteResult ({"nInserted": 1}) </span>

Update document db. COLLECTION_NAME.update (SELECTION_CRITERIA, UPDATED_DATA)
<Span style = "font-size: 14px;"> # change the e-mail of user_id = 2 to group@qq.com> db. jingdong. update ({"user_id": "02", "e-mail": "test@qq.com" },{ $ set: {"e-mail": "group@qq.com "}}) # The content of the first braces indicates the search condition, and the content of the second braces indicates the updated data WriteResult ({"nMatched": 1, "nUpserted": 1, "nModified ": 1})> db. jingdong. find () </span>

<Pre name = "code" class = "python"> <span style = "font-size: 14px;"> the default update function updates only one document, if you want to use all documents, you need to add multi: ture </span>
 
<span style="font-size:14px;">db.shiyanlou.update({"e-mail":"test@qq.com"},{$set:{"e-mail":"group@qq.com"}},{multi:ture})</span>

Replace the existing document db. COLLECTION_NAME.save ({_ id: ObjectId (), NEW_DATA })
<span style="font-size:14px;">> db.shiyanlou.save({"_id":ObjectId("53ea174ccb4c62646d9544f4","name":"Bob","position":"techer")})WriteResult({"nMatched":1,"nUpserted":1,"nModified":1})</span>

Delete document db. COLLECTION_NAME.remove (DELECTION_CRITERIA)
<span style="font-size:14px;">> db.shiyanlou.remove({"name":"Bob"})WriteResult({"nRemoved":1})</span>

Then you can use find () to view all documents and verify that the deletion is successful.

CreateCollection (): Creates a collection.

Db. COLLECTION. drop (): deletes a set.

Db. COLLECTION_NAME.insert (document): Insert document

Db. COLLECTION_NAME.update (SELECTION_CRITERIA, UPDATED_DATA): update document

Db. COLLECTION_NAME.save ({_ id: ObjectId (), NEW_DATA}): replace an existing document

Db. COLLECTION_NAME.remove (DELECTION_CRITERIA): delete a 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.