如何使用pymongo更新mongodb集合中的单个文档?

huangapple go评论141阅读模式
英文:

How to update a mongodb collection with single document using pymongo?

问题

如果我使用以下方式创建一个只有一个文档的集合:

db.time_data.insert({"Last_updated":"Jan 6 22:19:48 IST","Next_scheduled":"Jan 6 23:19:48 IST"})

我要如何更新 Last_updatedNext_scheduled 字段?
我希望在不使用任何选择器的情况下进行更新命令。在这里,选择器将自动生成。

英文:

If i create a collection with single document using:

db.time_data.insert({"Last_updated":"Jan 6 22:19:48 IST","Next_scheduled":"Jan 6 23:19:48 IST"})

How can i update the Last_updated and Next_scheduled fields?
I would like to do it without using any selector for update command. Here the selector would be auto generated id.

答案1

得分: 2

不确定您是如何创建文档的:

db.time_data.insert({"Last_updated":"Jan 6 22:19:48 IST"},{"Next_scheduled":"Jan 6 23:19:48 IST"})

因为这个操作会导致文档被创建成这样:

/* 1 */
{
    "_id" : ObjectId("5e1374692a0e7dc716ae47fa"),
    "Last_updated" : "Jan 6 22:19:48 IST"
}

这是正常的,因为第二个对象 { "Next_scheduled": "Jan 6 23:19:48 IST" } 被忽略了。您的查询应该像下面这样,以创建一个包含两个字段的单个文档:

db.time_data.insert({"Last_updated":"Jan 6 22:19:48 IST","Next_scheduled":"Jan 6 23:19:48 IST"})

更新:

由于您的集合只有一个文档,要更新它,您实际上不需要任何过滤器,只需执行以下操作:

db.time_data.update({}, {"Last_updated":"Jan 7 22:19:48 IST","Next_scheduled":"Jan 7 23:19:48 IST"})

(或)

db.time_data.updateOne({}, {$set :{"Last_updated":"Jan 8 22:19:48 IST","Next_scheduled":"Jan 8 23:19:48 IST"}})

(或)

db.time_data.findOneAndUpdate({}, {$set :{"Last_updated":"Jan 9 22:19:48 IST","Next_scheduled":"Jan 9 23:19:48 IST"}})

因此,您可以使用上述任何一种方法,甚至是 .updateMany(),具体取决于您的需求。请查看它们的响应,有些返回写入结果,有些返回更新后的文档(如果设置了返回新文档的选项)。这些查询适用于MongoDB shell,请查找 pymongo Ref 链接中的 update 以将其转换为与Python一起使用的代码(最基本的更改将涉及语法,如将 updateOne 更改为 update_oneupdateMany 更改为 update_many,还要检查传递给这些操作的选项,因为它们可能从MongoDB shell到驱动程序(如 pymongo / mongoDB driver / mongoose)有所不同)。

MongoDB Ref: update.findOneAndUpdate()

pymongo Ref: pymongo-doc

英文:

Not sure how you've created a document using :

db.time_data.insert({"Last_updated":"Jan 6 22:19:48 IST"},{"Next_scheduled":"Jan 6 23:19:48 IST"})

cause that operation will result in document being created like this :

/* 1 */
{
    "_id" : ObjectId("5e1374692a0e7dc716ae47fa"),
    "Last_updated" : "Jan 6 22:19:48 IST"
}

It's quiet normal as second object {"Next_scheduled":"Jan 6 23:19:48 IST"} is ignored, your query should be like below in-order to create a single document with two fields :

db.time_data.insert({"Last_updated":"Jan 6 22:19:48 IST","Next_scheduled":"Jan 6 23:19:48 IST"})

Update :

Since your collection has only one document, to update it you don't really need any filter, you just do :

db.time_data.update({}, {"Last_updated":"Jan 7 22:19:48 IST","Next_scheduled":"Jan 7 23:19:48 IST"})

(Or)

db.time_data.updateOne({}, {$set :{"Last_updated":"Jan 8 22:19:48 IST","Next_scheduled":"Jan 8 23:19:48 IST"}})

(Or)

db.time_data.findOneAndUpdate({}, {$set :{"Last_updated":"Jan 9 22:19:48 IST","Next_scheduled":"Jan 9 23:19:48 IST"}})

So you could use any of above or even .updateMany() depends on need, check for their responses some return write result Vs some do return updated document(If you set option to return new document). These queries are for mongoDB shell, please search for update in pymongo Ref link to get these converted to work with python(Most basic changes will be syntax thing like updateOne to update_one, updateMany to update_many, also do check on options being passed with these as they may vary from mongoDB shell to drivers like pymongo/mongoDB driver/mongoose).

MongoDB Ref : update, .findOneAndUpdate()

pymongo Ref : pymongo-doc

huangapple
  • 本文由 发表于 2020年1月7日 00:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定