The array key for the MONGODB bulk Update operation document

Source: Internet
Author: User

The data for the persons document is as follows:

> Db.persons.find ()
{"_id": 2, "Name": 2}
{"_id": 3, "Name": 3}

> db.persons.update ({_id:4},{_id:4,name:4})
Writeresult ({"nmatched": 0, "nupserted": 0, "nmodified": 0})
> Db.persons.find ()
{"_id": 2, "Name": 2}
{"_id": 3, "Name": 3}

After you finish the update operation, you still cannot see the _id:4 record, because the Update method requires a true indicator to insert the records that are not queried:

> db.persons.update ({_id:4},{_id:4,name:4},true)
Writeresult ({"nmatched": 0, "nupserted": 1, "nmodified": 0, "_id": 4})
> Db.persons.find ()
{"_id": 2, "Name": 2}
{"_id": 3, "Name": 3}
{"_id": 4, "Name": 4}

Existing requirements, change the name "3" in the persons document to "33"

> db.persons.update ({name: "3"},{$set: {name: "}},false,true")

False meaning: If the name: "33" key value pair is not found, then insert operation is not performed, true meaning: Indicates a bulk update

Add Age to persons: "88" property

> db.persons.update ({name: "4"},{$set: {age: "88"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4", "Age": "88"}

Add age 2

> db.persons.update ({name: "4"},{$inc: {age:2}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4", "Age": 90}

Take the Age attribute away:

> db.persons.update ({age:90},{$unset: {age:1}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}

Add a record to the persons document, _id to 5

> Db.persons.insert ({_id:5,name:5,books:[]})
Writeresult ({"ninserted": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "name": 5, "books": []}

Add an element to the books array: "JS" and "extjs4.0"

> db.persons.update ({_id:5},{$push: {books: "JS"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "name": 5, "Books": ["JS"]}

> db.persons.update ({_id:5},{$push: {books: "extjs4.0"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "name": 5, "Books": ["JS", "extjs4.0"]}

Create a new classes array:

> db.persons.update ({_id:5},{$push: {classes: "01class"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "name": 5, "Books": ["JS", "extjs4.0"], "classes": ["01class"
] }

Add several elements at a time for the calsses array:

> db.persons.update ({_id:5},{$pushAll: {classes:["02class", "03class", "04class"]}}
)
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "name": 5, "Books": ["JS", "extjs4.0"], "classes": ["01class"
, "02class", "03class", "04class"]}

Deleting _id is a record of 5 and creating a new _id is a record of 5, using $addtoset, this command checks whether the element to be added is present in the array, the presence will not be added, otherwise it will be added:

> Db.persons.remove ({_id:5})
Writeresult ({"nremoved": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
> Db.persons.insert ({_id:5,books:["JS"]})
Writeresult ({"ninserted": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["JS"]}
> db.persons.update ({_id:5},{$addToSet: {books: "JS"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 0})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["JS"]}
> db.persons.update ({_id:5},{$addToSet: {books: "Java"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["JS", "Java"]}
> db.persons.update ({_id:5},{$addToSet: {books: "Mongo"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["JS", "Java", "MONGO"]}

Delete the first element of the books array: "JS", using the $pop command:

> db.persons.update ({_id:5},{$pop: {books:-1}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["Java", "MONGO"]}
> db.persons.update ({_id:5},{$pop: {books:1}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["Java"]}

-1 for the first element, 1 for the last element

You can also use the pull command to delete a specified element at a time:

> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["Java", "Mongo", "JS"]}
> db.persons.update ({_id:5},{$pull: {books: "JS"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "Books": ["Java", "MONGO"]}

$PULLALL command to specify multiple elements to be deleted at one time:

> db.persons.update ({_id:5},{$pullAll: {books:["java", "Mongo"]}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "books": []}

Create a new record _id to 6:

> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "books": []}
{"_id": 6, "books": [{"Type": "JS", "name": "extjs4.0"}, {"Type": "DB"
, "name": "MongoDB"}, {"Type": "JS", "Name": "jquery"}]}

Add pens: "Too long" attribute to the books element of type JS, use the. Symbol must be quoted with double quotes

> db.persons.update ({"Books.type": "JS"},{$set: {"Books.$.author": "Tom"}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4, "name": "4"}
{"_id": 5, "books": []}
{"_id": 6, "books": [{"Type": "JS", "name": "extjs4.0", "Author": "Tom"
}, {"Type": "DB", "Name": "MongoDB"}]
}

Db.persons.update ({"Books.type": "JS"},{$set: {"Books.$.pens": "Too Long"}})

The array element is judged to perform an insert operation, and the repeating element is not inserted a second time:

> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4}
{"_id": 5, "Books": ["JS"]}

> db.persons.update ({_id:5},{$addToSet: {books:{$each: ["JS", "DB", "Java"]}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.persons.find ()
{"_id": 2, "name": "33"}
{"_id": 3, "name": "33"}
{"_id": 4}
{"_id": 5, "Books": ["JS", "DB", "Java"]}

The array key for the MONGODB bulk Update operation 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.