Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement an update function in MongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

MongoDB in how to achieve an update function, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

In MongoDB, there are two types of functions for updating data: save () and update ().

The most direct function to use if you want to modify the data is the update () function, but the syntax of this function is cumbersome.

Grammar: db. Collection .update (update condition, new object data (update operator), upsert,multl)

● upsert: if the data to be updated does not exist, add a new item (true is added, false is not added).

● multi: indicates whether to update only the first row of records that meet the criteria, if set to false, only update the first entry, and if set to true full update.

Example: update existing data-update salary at the age of 30 to 8000 (multiple data will be returned at this time)

Update only the first piece of data:

> db.emp.find () .skip (0) .limit (5) .sort ({"$natural": 1}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be26")

"name": "Zhao Yi"

"sex": "male"

"age": 30

"sal": 1000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be27")

"name": "Qian er"

"sex": "female"

"age": 22

"sal": 5000

"loc": "Shanghai"

}

{

"_ id": ObjectId ("599108423268c8e84253be28")

"name": "Sun San"

"sex": "male"

"age": 40

"sal": 2000

"loc": "Shenzhen"

}

{

"_ id": ObjectId ("599108423268c8e84253be29")

"name": "Li Si"

"sex": "female"

"age": 30

"sal": 7000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be2a")

"name": "Friday"

"sex": "female"

"age": 30

"sal": 6400

"loc": "Beijing"

}

> db.emp.update ({"age": 30}, {"$set": {"sal": 9999}}, false,false)

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

> db.emp.find () .skip (0) .limit (5) .sort ({"$natural": 1}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be26")

"name": "Zhao Yi"

"sex": "male"

"age": 30

"sal": 9999

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be27")

"name": "Qian er"

"sex": "female"

"age": 22

"sal": 5000

"loc": "Shanghai"

}

{

"_ id": ObjectId ("599108423268c8e84253be28")

"name": "Sun San"

"sex": "male"

"age": 40

"sal": 2000

"loc": "Shenzhen"

}

{

"_ id": ObjectId ("599108423268c8e84253be29")

"name": "Li Si"

"sex": "female"

"age": 30

"sal": 7000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be2a")

"name": "Friday"

"sex": "female"

"age": 30

"sal": 6400

"loc": "Beijing"

}

All data that meets the criteria is updated:

> db.emp.update ({"age": 30}, {"$set": {"sal": 9999}}, false,true)

WriteResult ({"nMatched": 5, "nUpserted": 0, "nModified": 4})

> db.emp.find () .skip (0) .limit (5) .sort ({"$natural": 1}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be26")

"name": "Zhao Yi"

"sex": "male"

"age": 30

"sal": 9999

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be27")

"name": "Qian er"

"sex": "female"

"age": 22

"sal": 5000

"loc": "Shanghai"

}

{

"_ id": ObjectId ("599108423268c8e84253be28")

"name": "Sun San"

"sex": "male"

"age": 40

"sal": 2000

"loc": "Shenzhen"

}

{

"_ id": ObjectId ("599108423268c8e84253be29")

"name": "Li Si"

"sex": "female"

"age": 30

"sal": 9999

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be2a")

"name": "Friday"

"sex": "female"

"age": 30

"sal": 9999

"loc": "Beijing"

}

Example: update data that does not exist

> db.emp.update ({"age": 55}, {"$set": {"name": "does not exist"}, true,false)

WriteResult ({

"nMatched": 0

"nUpserted": 1

"nModified": 0

"_ id": ObjectId ("5991629aca6455d4a46870f6")

})

> db.emp.find ({"age": 55}) .pretty ()

{"_ id": ObjectId ("5991629aca6455d4a46870f6"), "age": 55, "name": "does not exist"}

At this point, it is equivalent to creating the data.

Then in addition to update (), a save () function is provided, which is similar to updating content that does not exist.

Example: using the save () operation

> db.emp.find ({"age": 55}) .pretty ()

{"_ id": ObjectId ("5991629aca6455d4a46870f6"), "age": 55, "name": "does not exist"}

> db.emp.save ({"_ id": ObjectId ("5991629aca6455d4a46870f6"), "age": 56})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

> db.emp.find ({"age": 56}) .pretty ()

{"_ id": ObjectId ("5991629aca6455d4a46870f6"), "age": 56}

> db.emp.save ({"_ id": ObjectId ("5991629aca6455d4a46870f6"), "age": 56, "name": "stone"})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

> db.emp.find ({"age": 56}) .pretty ()

{

"_ id": ObjectId ("5991629aca6455d4a46870f6")

"age": 56

"name": "stone"

}

With save, only the fields that appear in the save are preserved. If the data does not exist, insert it.

> db.emp.save ({"age": 58, "name": "stone1"})

WriteResult ({"nInserted": 1})

> db.emp.find ({"age": 58}) .pretty ()

{

"_ id": ObjectId ("599164db0184ff511bf02b96")

"age": 58

"name": "stone1"

}

The corresponding id data exists, which is the update operation. The data to be saved does not exist ("_ id" cannot be saved), which becomes an increment operation.

It is recommended that you use update and use save as little as possible.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report