Persons data for this file 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 the update operation, the _id:4 record is still not visible. Because the Update method requires a true indicator. To insert operations on records that cannot be 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 of "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 the insert operation is not run, and true means: Indicates a bulk update
Add Age: "88" Property for persons
> 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, and the presence is not added. Otherwise you will join:
> 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". Use 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 use a double-quoted reference
> 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 inferred array element runs the 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"]}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
MongoDB Bulk update of the array's key operations file