MongoDB data update method dry article

  • 2020-06-12 10:54:23
  • OfStack

preface

Data update is an essential part of our daily operation database, the following article will give you to share the operation of MongoDB data update 1 dry goods, you have a certain reference learning value, 1 up to learn.

Common functions

update(<query>,<update>,<upsert>,<multi>) , including < query > Represents the filter condition, < update > It's data to be updated updateMany() Update all matched data

upsert

upsert is a Boolean data, if true, when no matching data is found according to the query condition, the data is inserted, if false, the data is not inserted

The following will update the data in an empty collection


// This data is inserted because no matching information is found 
db.user.update({'name':'chenjiabing','age':22,'sex':"Man"},{$set:{'hobby':'read'}},{'upsert':true}); 
db.user.update({'name':'chenjiabing','age':22,'sex':"Man"},{$set:{'hobby':'read'}},true); // This is equivalent to the statement above 
// The output  db.user.find()
{ "_id" : ObjectId("59067b70856d5893a687655f"), "age" : 22, "name" : "chenjiabing", "sex" : "Man", "hobby" : "read" }

multi

If this parameter is true, multiple records are updated by condition lookup. Default is false, if true and updateMany() The effect of 1 sample

All matched data will be updated below


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});

Field update operator Field Update Operators

$set

$set Used to specify the value of 1 key. If the key does not exist, create it. Note that the default update here is to update only the data matched in item 1. If the data matched in item 1 already meets the modified condition, the following matching information will not be performed

Next we will add 1 piece of information to the database


db.user.insert({"name":'jack',"age":22,"sex":'Man','school':{'name':'jsnu','city':'xuzhou'}});

Run the following code, set the user's interest to "Read" and add it to the document (the document will create the hobby key if it does not exist)


db.user.update({name:'jack'},{$set:{'hobby':'read'}})

The user's age will be changed below


db.user.update({'name':'jack'},{$set:{'age':20}})

Change the data type with $set to set sex to 1


db.user.update({'name':'jack'},{$set:{'sex':1}})

Next, modify the embedded document with $set. You must specify the name and key value of the document


db.user.update({name:'jack'},{$set:{'school.name':'shida','school.city':'beijing'}})

$unset

Removes the specified key from the document

The hobby key inserted above will be deleted next


db.user.update({name:'jack'},{$unset:{'hobby':1}}) // I'm going to give you whatever value you want, whatever value you want 

$inc

$inc Modifiers are used to increase the value of an existing key or to create a key if one does not exist $inc It's designed to increase (and decrease) Numbers. $inc Can only be used with integer, long integer, or double - precision floating - point Numbers. If used on other types of data, the operation will fail

For example, the number of views on the blog will be increased by 1 for each person visiting the blog, and the key pageViews will be used to save the views. This key value is not defined above, so 1 is automatically created


db.user.update({name:'jack'},{$inc:{'pageViews':1}}); // It's automatically created if it's not there at first 1 A key 

The following shows the increase and decrease


db.user.update({name:'jack'},{$inc:{'pageViews':100}}) ; // This is just adding to the top 100 Theta is now theta 101
db.user.update({name:'jack'},{$inc:{"pageViews":-100}}) ; // Here we're subtracting from the top 100, So this is going to be the same 1

$rename

Grammar: {$rename: { <old name1>: <new name1>, <old name2>: <new name2>, ... } }

$rename The operator can rename the field name; the new field name cannot be the same as the existing field name in the document.

Next, re-insert 1 piece of data and change the name of its key


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});
0

The following shows how to change the key name of the embedded document. Note that 1 must include the document name


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});
1

If the renamed field word is the same as the original field name in the collection, the original field name will be overwritten and the data will be lost


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});
2

If the specified field does not exist, it is not updated and has no effect on the original field


db.user.update({name:'chenjiabing'},{$rename:{value:'name'}}); // There won't be any change because value This field doesn't exist 

$rename The operator can also move key values from a subdocument to other subdocuments


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});
4

The array update operator Array Update Operators

An array operation that can only be used on keys with an array of key values.
$ (query)

Grammar: updateMany()0

When updating an array field without explicitly specifying the location of the element in the array, we use the location operator $to identify an element, and the Numbers start with 0.

Note:

The location operator (" $") serves as a placeholder for the first element that matches the query criteria, that is, the index value in the array. Array fields must appear in the query document.

Insert two pieces of data into the collection


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});
5

Do the following


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});
6

$push

If the specified key already exists, an element is added to the end of the existing array; if not, a new array is created.

We will use the following $push Add a comment to the document.


db.user.update({name:'chenjiabing'},{$set:{'hobby':'code'}},{'multi':true});
7

$pull

Grammar: db.collection.update( { field: <query> }, { $pull: { field: <query> } } );

$pull The $pull operator removes the specified field value as an array and matches all elements of the query condition declared by the $pull statement.

Do the following


// insert 1 A document 
db.profiles.insert({ votes: [ 3, 5, 6, 7, 7, 8 ] });
// Removes all elements from the array 7
db.profiles.update( { votes: 3 }, { $pull: { votes: 7 } } );
// Removes any greater than in the array 6 The elements of the 
db.profiles.update( { votes: 3 }, { $pull: { votes: { $gt: 6 } } } );
//Result
{ votes: [ 3, 5, 6, 8 ] }
{ votes: [ 3, 5, 6 ] }

conclusion


Related articles: