In the previous article, we briefly introduced some basic operations commands, which are now described in more detail for each of these commands:
First, data insertion
Inserting data using the command Insert,insert has only one parameter, which is the document Bson data to be inserted. MongoDB's shell client does not provide a Bulk INSERT API, and if you need to bulk insert it, you need to insert the data using JavaScript scripts.
Db.users.insert ({name: "Tom", pwd: "123", Sex: "M"}) # # # # # # # # # # # # separate Insert data
for (Var i=0;i<100;i++) {Db.users.insert ({name: "Tom" +i,pwd: "" +i})} # # # # # # # # # # # Insert Data
In addition to inserting data using INSERT, you can insert data using the Save command, and save has only one parameter.
Db.users.save ({_id:objectid ("012345678901234567890123"), Name: "Lili"})
The difference between save and insert: When the unique index key is the same, the save operation is forced to update, that is, delete in the rebuild, but insert will error.
Db.users.insert ({_id:objectid ("012345678901234567890123"), Name: "Lili2", pwd: "123"}) # # # # # # # # # # # of exceptions, E11000 Duplicate key error Indexdb.users.save ({_id:objectid ("012345678901234567890123"), Name: "Lili2", pwd: "123"})
Second, delete the document
Delete a document only the command Remove,remove has only one parameter, and the Remove method deletes all matching documents. If you need to delete all the document data in the collection, create a direct use of the drop method.
Db.users.remove ({name: "Lili2"}) # # # Delete the user named Lili2
Third, update the document
The update document command for the Update,update method parameter format is:
Db. [Collectionname].update (Finder, updater, whether to insert, whether in bulk)
By default, the update operation is only updated when the data is queried by the Finder, so you can set the insert if no data is present (the third parameter is true); The update operation updates only the first matching record, and you can set the fourth parameter to true for bulk updates.
If we do not use the modifier in the updater, then we are forced to update, that is, to delete the action added (a record), and if you use the fourth parameter, then you must use the modifier:
Db.users.update ({name: "Lili"},{age:23}) # # # # # # # # # # # # # # # # # # # # # # # #
Db.users.update ({name: "Lili"},{$set: {age:23}}) # # # # OK, add an age
Common modifiers:
Modifier name |
Grammar |
Case |
$set |
{$set: {field:value[,field2:value2 ...]}} |
Db.users.update ({},{$:{name:1}}) |
$set is used to specify a key-value pair that is modified if the key exists and then inserted if it does not exist. |
$inc |
{$inc: {Field:value}} |
Db.users.update ({},{$inc: {age:1}}) |
$inc can only be used in conjunction with numeric types to add/or reduce operations for numeric values of the number type that corresponds to the specified key . |
$unset |
{$unset: {field:1}} |
Db.users.update ({},{$unset: {email:1}}) |
Delete the specified key |
$push |
{$push: {Field:value}} |
Db.users.update ({},{$push: {books: ""}}) |
1. If the specified key is an array, append the value to the array. 2, if the specified key is not an array, then interrupt the current operation cannot apply $push/$pushAll modifier to Non-array 3.If the specified key does not exist, create a new array key value pair for the specified key |
$pushAll |
{$pushAll: {Field:array}} |
Db.users.update ({},{$push: {books:[]}}) |
Use and push , insert all data |
$addToSet |
{$addToSet: {Field:value}} |
Db.users.update ({},{$addToSet: {ips: "127.0.0.1"}}) |
If the key does not exist, it is created. If the key exists, then the specified value is judged whether it is in the value, if it is, then no operation, if not, then add the entry. If the specified key is not a key of type array , then an error is interrupted. |
$pop |
{$pop: {Field:value}} |
Db.users.update ({},{$pop: {books:1}}) |
Removes a value from the specified array,1 means that the last value is deleted, and1 means that the first value is deleted. |
$pull |
{$pull: {Field:value}} |
Db.users.update ({},{$pull: {Books "" JS "}}) |
Deletes a specified value |
$pullAll |
{$pullAll: {Field:array}} |
Db.users.update ({},{$pullAll: {books:["JS", "MONGO"]}}) |
Delete multiple values at once |
$ |
|
|
Array locator, if the array has multiple values, but we want to modify only a subset of the data, then we may need to use the locator ($). For example, when there is a document{name:"YFC", Age:27,books:[{type:'JS', Name:"EXTJS4"},{type:"JS", Name:"JQUERY"},{type:"DB", Name:"MONGODB"}]}we're going to puttypeto beJSadd an identical author to the documentAuthis aUpscat Way: Db.users.update ({"Books.type": "JS"},{$set: {"Books.$.author": "Uspcat"}}) |
$each |
{$each: Array} |
|
$each The elements in the array to a single loop, you can go to the $addToSet modifier mates to complete, add the non-existent elements, the existing elements do not add functionality. That is $addToSetAll function ( This modifier does not exist ). Db.users.update ({},{$addToSet: {books:{$each: [{type: "java", Name: "Spring"},{type: "DB", Name: "MongoDB"}]}}},false, True |
[MongoDB]-Data deletion and modification