MongoDB document field changes and additions

Source: Internet
Author: User
Tags modifier modifiers mongodb collection mongodb documentation

MongoDB is based on crud (Create,read,update,delete) method to implement the collection of documents on the increase and deletion of the search. For the additions and deletions of the fields on the collection, you can use set or unset modifiers to implement them. You can also do this by using the document substitution method. This article mainly describes the additions and deletions of the fields on the collection, and updates based on option Upsert.

A.Syntax Description

Db.collection.update (

<query>,//query or filter conditions

<update>,//modifier (modifier keys and contents)

{

Upsert: <boolean>,//Is TRUE or false, if true, a new document is created if no matching document is found

Multi: <BOOLEAN>,//To determine if all rows are single row or update (true for all rows)

Writeconcern: <document>//Setting write concerns to ensure strong consistency or weak consistency

The syntax parameters are basically the same after 3.2}//After

)

For other usages such as Updateone,updatemany, please refer to: MongoDB documentation update

All the write operation characteristics on the MongoDB collection

Atomic Operations (single-document-level atomic operations)

The _id field cannot be modified, i.e. a new _id value cannot be used in place of

The fill factor is automatically adjusted and the space is redistributed because the update causes the document size to exceed the expected allocation

Preserves the order of document fields, but updating or renaming may cause the field order to be reordered (_id is always the first field in the document)

Several common modifiers for update:

$set modifier most commonly used, equivalent to the SET clause of Rdbmsupdate

B.Use$setModifier Modify Age field

> db.chenji.update ({name: "Li Xu"},{$set: {"Age": 24}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

" Age ": " third grade ",

" Age ": 24

}

> Db.chenji.find ({$or: [{name: " li Xu "},{" Age ": 24}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

" Age ": " third grade ",

" Age ": 24

}

C.New Document Fields($setImplement)

> db.chenji.update ({name: " li Xu "},{$set: {add: "Sex"}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: " li Xu "},{" Age ": 24}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

" Age ": " third grade ",

" Age ": 24,

"Add": "Sex"

View new Fields Add

> Db.chenji.find ({name: " li Xu "},{"_id": 0, " old ": 1, " Age ": 1,add:1})

{" Age ": " third grade ", " Age ": +, "add": "Sex"}

D.Document Delete Field

the deletion method for the field is {"$unset": {field_name:1}}

> db.chenji.update ({name: " li Xu "},{"$unset": {add:1}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

" Age ": " third grade ",

" Age ": 24

}

> Db.chenji.find ({name: " li Xu "},{"_id": 0, " old ": 1, " Age ": 1,add:1})

{" Age ": " third grade ", " Age ": 24}

Delete Age field:

> db.chenji.update ({name: " li Xu "},{"$unset": {" age ": 1}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

" Age ": " third grade "

}

Delete Age field:

> db.chenji.update ({name: " li Xu "},{"$unset": {" age ": 1}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]})

{"_id": ObjectId ("59987EEE7CD6CDA607043CD1"), "name": " li Xu "}

>

Add field: Age and ages

> db.chenji.update ({name: " li Xu "},{$set: {" grade ": " First grade "}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> db.chenji.update ({name: " li Xu "},{$set: {"Age": 9}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

"Age": 9,

" grade ": " first grade "

}

E.increase or decrease of field values

When the $inc modifier is used, the field is automatically created when the field does not exist and, if present, increases or decreases based on the original value

$inc is primarily used to increase or decrease numbers specifically, so $inc can only be used for integer, long, or double-precision floating-point values

$inc does not support strings, arrays, and other non-numeric values

Note that for $inc operation, $set can also be completed. The reason $inc exist is that $inc is more efficient

> db.chenji.update ({name: "Li Xu"},{$inc: {num:100}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

"Age": 9,

" grade ": " first grade ",

"Num": 100

}

Update again this num field will increase by 101 for the first TIME 100

> db.chenji.update ({name: " li Xu "},{$inc: {num:101}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]})

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

"Age": 9,

" grade ": " first grade ",

"Num": 201

}

based on $ Inc Negative value of :

> db.chenji.update ({name: " li Xu "},{$inc: {num:-100}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]}). Pretty ()

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

"Age": 9,

" grade ": " first grade ",

"Num": 101

}

> db.chenji.update ({name: " li Xu "},{$inc: {num:-50}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]}). Pretty ()

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

"Age": 9,

" grade ": " first grade ",

"Num": 51

The following uses non-numeric to implement $ Inc , the error is as follows :

Db.chenji.update ({name: " li Xu "},{$inc: {num: "1ab"}})

Writeresult ({

"nmatched": 0,

"nupserted": 0,

"Nmodified": 0,

"Writeerror": {

"Code": 14,

"ErrMsg": "Cannot increment with non-numeric argument:{num: \" 1ab\ "}"

}

})

F. Increase and automatic update of timestamp fields ($currentDate)

> db.chenji.update ({name: " li Xu "},{$inc: {num:60}, $currentDate: {lastmodified:true}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]}). Pretty ()

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Name": " li Xu ",

"Age": 9,

" grade ": " first grade ",

"num": 231,

"LastModified": Isodate ("2017-08-20t08:39:53.601z")

}

F.document Field renaming($rename)

> db.chenji.update ({name: " li Xu "},{$rename: {"name": "Ename"}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.chenji.find ({$or: [{name: ' li Xu '}]}). Pretty ()

> Db.chenji.find ({$or: [{ename: " li Xu "}]}). Pretty ()

{

"_id": ObjectId ("59987EEE7CD6CDA607043CD1"),

"Age": 9,

" grade ": " first grade ",

"num": 231,

"LastModified": Isodate ("2017-08-20t08:39:53.601z"),

"Ename": " li Xu "

}

> Db.chenji.find ({ename: " li Xu "},{"_id": 0,ename:1})

{"ename": " li Xu "}

Rename all document fields on the entire collection :

>db.chenji.update ({},{$rename: {"name": "Ename"}},{multi:true})

Writeresult ({"nmatched": 7, "nupserted": 0, "nmodified": 6}) / This is modified as One, because the previous and modified 1 Strip

> Db.chenji.find ();

{"_id": ObjectId ("59990ec402730a37fe1ee79e"), " Age ": " second grade ", " Age ": "Two", "ename": " Jian Wei " }

{"_id": ObjectId ("59987EEE7CD6CDA607043CD1"), "age": 9, " grade ": " first grade ", "num": 231, "lastmodified": ISOD Ate ("2017-08-20t08:39:53.601z"), "ename": " li Xu "}

{"_id": ObjectId ("5998860A7CD6CDA607043CD2"), " Age ": " third grade ", " Age ": "+", "ename": " Zhang San "}

{"_id": ObjectId ("599886227CD6CDA607043CD3"), " Age ": " grade Four ", " Age ": "+", "ename": " Wang San " }

{"_id": ObjectId ("599886357cd6cda607043cd4"), " Age ": " first grade ", " Age ": "8", "ename": " Wang Qi " }

{"_id": ObjectId ("599886537CD6CDA607043CD5"), " Age ": " first grade ", " Age ": "9", "ename": " pap" /c11> "}

{"_id": ObjectId ("5998866e7cd6cda607043cd6"), " Age ": " Seven grade ", " Age ": "+", "ename": " Sheenah " }

>

G.upsertoption Usage

/Upsert equivalent to Oracle's merge into or MySQL Replaceinto

Upsert is when a document is matched to a document that meets the criteria in the collection, or a new document is added. If the value of this option is true, the default is Flase.


>//The following demo is an example of matching to a document

Summary:

A, there are many ways to modify the data on the document (modifier), commonly used for $set modifier and $inc

B, $inc is an efficient data modifier that is typically used to increase or decrease the number of values, supporting only data types.

C, for the increase of the document field, you can use $set, $unset, $inc, $currentDate and other ways

D, for the deletion of the document field, use the $unset method to implement

E, Upsert option to implement matching document is updated, does not match when inserting

Resources:

http://blog.csdn.net/leshami/article/details/54930588

This article is from the "10931853" blog, please be sure to keep this source http://wujianwei.blog.51cto.com/10931853/1957832

MongoDB document field changes and additions

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.