1024programmer Mongodb How to use MongoDB’s insert and update updates

How to use MongoDB’s insert and update updates

When we use MongoDB’s update, if we set the third parameter to true, a new document will be created if no document is found. The following is an example:

>db.foo.find()

{ “_id” : ObjectId(“4e9165cf717ed94f8289ac0c”), “bar” :
“baz”}

>db.foo.update({“bar”:”baz”}, {“$inc”:{“count”:3}})

>db.foo.find()

{ “_id” : ObjectId(“4e9165cf717ed94f8289ac0c”), “bar” :
“baz”, “count” : 3 }

>db.foo.update({“bar”:”bazz”}, {“$inc”:{“count”:3}})

>db.foo.find()

{ “_id” : ObjectId(“4e9165cf717ed94f8289ac0c”), “bar” :
“baz”, “count” : 3 }

> db.foo.update({“bar”:”bazz”},
{“$inc”:{“count”:3}},true)

> db.foo.find()

{ “_id” : ObjectId(“4e9165cf717ed94f8289ac0c”), “bar” :
“baz”, “count” : 3 }

{ “_id” : ObjectId(“4e916661739f1da5452a4dfe”), “bar” :
“bazz”, “count” : 3 }

If the third parameter is not set to true in update, no action will be taken when the document to be updated cannot be found; if the third parameter is set to true, a new document will be created if no document is found.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-use-mongodbs-insert-and-update-updates/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索