Explanation of update operation of MongoDB database
I didn’t record the update operation before, so I have time to record the use of mongodb update today Look at the syntax first: db.collection.update( , , ) The syntax is very simple, but it will be more complicated to use with some functions. Let me explain. query: conditional limitation, that is, the condition that the row needs to be updated, which is equivalent to the conditional judgment after where in SQL update: equivalent to the set statement in the SQL table options: What needs to be noted here is the update operation of mongodb. By default, only the first row that meets the conditions is updated. If you want to update all the rows that meet the conditions, you need to specify multi to be true, and one parameter is upsert, which means if it does not exist The records that match the query are inserted into the updated data. The default is false and not inserted. You can specify true The simplest example, let’s take a look: PRIMARY> db.test.save({id:1,”name”:”hank”}) PRIMARY> db.test.find() { “_id” : ObjectId(“50c055656b41f59a0235881b”), “id” : 1, “name” : “hank” } PRIMARY> db.test.update({id:1},{“name”:”dazuiba”}) PRIMARY> db.test.find() { “_id” : ObjectId(“50c055656b41f59a0235881b”), “name” : “dazuiba” } An example of updating…