This blog post mainly studies data update operations for document in MongoDB. Includes the data "Insert", "Update" and "delete".
Document Data Insertion
The command to insert a piece of data into the document file is "db. Collection name. Insert (ON=BJ)",:
You can see that after I insert a piece of data, the query discovery is actually added to the collection.
So what should I do if I need to insert data in batches?? As we have said before, the client shell of MongoDB is actually a JavaScript engine. So we can use the For loop to perform the operation.
For example, I use the For loop to achieve the effect of bulk insert data.
Note:
There is also a Save method in the insert in MongoDB, so what is the difference between him and the Insert method?? When should I use save and when should I use Insert??
When we do the insert operation, if we use the Save method, when we encounter "_id" the same case, the Save method completes the saving operation, and if the Insert method is used, an error occurs.
Document Data Deletion
Delete all data in a list
If I need to delete all the data from the list, I can write:
Db.students.remove ({})
Note that this only deletes the data, and the index is not deleted.
to delete data based on criteria:
If you need to delete data from a collection based on conditions, you can write:
Db.students.remove ({condition})
As you can see, I first added two data and then deleted the age=11 data, and again the discovery was actually deleted.
Update operation
The Update method, as the name implies, is when I execute the method, will first based on the method query criteria query, if the query to, then perform the update operation, if no query to, then perform the insert operation.
Syntax: DB. Collection name. Update ({finder},{modifier},true)
Note that if you want the update method to have such an attribute, then the third parameter must be filled with true.
You can see that when my third parameter is set to True, at this point, when I update a age=33 data, I do an insert operation when I do not find the data.
Note:
If I update the data using the Update method, MongoDB will only update the first data for us by default if there are more than one data at this time to satisfy the finder. Like what:
Can see: I update Name= "WANGWU" data, found only the first to meet the conditions of the data update, then if I need a batch update, how should I do??
It can be written like this:
db.students.update ({query condition},{$set: {Update Data}}, whether insertorupdate, bulk update)
You can see that the $set modifier is used here. Such as:
MongoDB modifier
MongoDB provides us with a lot of modifiers for implementing data modifications, and here's a list of MongoDB modifiers I found on the web.
$set: Used to specify a key-value pair, modify if it exists, and add if it does not exist . Such as:
According to the picture, I first for name= "xiaoming" update a age=44 data, this time because the data does not have this age key, so the data will be added to the AGE=44 key value pairs, but when I name= "Lisi" Data set age=25, Since this data already has the age key, the key value pair is updated.
$inc: Used to add and subtract numeric values for a specified key, such as the following:
, as can be seen, I name= "xiaoming" This data, using the $inc modifier to the data of the age key value +2, if necessary minus 2, then fill 2.
$unset: Deletes the specified key, such as:
As you can see, we have successfully removed the name= of the "xiaoming" data.
$push Modifiers
$push modifier:
1. If the specified key is an array, append a new value.
2. If the specified key is not an array, the current operation is interrupted.
3. If the specified key does not exist, create a key-value pair of the array type.
At this time I use the $push modifier to perform two update operations, the first, there is no scores this key, so the creation of an array type of key-value pairs, the second time, because already exists this key, so will continue to append.
$pushAll Modifiers
PushALLRepairChangeDeviceof thewithMethodand the The push modifier is similar, and he is used to batch add array data, such as I need to give name= "xiaoming" document to add a Fruits[apple,orange,watermalon] such data,
$addToSet Modifiers
$pushAll: When there are values in the array that need to be added, do nothing and add if there are no values in the array that need to be modified.
As you can see, when I first passed the $addtoset modifier and added a value of 100 to the scores array, since this value already exists, nothing has been done, and when I add the value for the second time it is 88 data that is added at this point.
$pop Modifiers
$pop modifier: Removes a value from the specified array: 1 represents the last value, and 1 represents the first value.
As you can see, when I delete the data in the fruits array, 1 means delete the last one, 1 means delete the first one.
$pull Modifiers
$pull modifier: Used to delete a specified value
I am here, in the execution $pull modifier delete fruits in orange this is worth before and after, respectively executed "db.student.find ()" Discovery has been successfully deleted.
$pullAll Modifiers
$pullAll modifier: Used to delete all data in an array
OK, today I learned about the operation part of the document data in MongoDB.
MongoDB Learning (document data manipulation)