繁体   English   中英

MongoDB 忽略 $push 命令而没有错误 | Python

[英]MongoDB ignoring $push command wihout errors | Python

我有包含数组字段和第一个值的文档。

我想更新这个文档并在数组中添加值,所以我通过 find_one 命令获取文档并使用以下代码:

mg[db][collection].update_one(document, {"$push": {array_field: value} })
mg.close()
print("end")

所以,我可以看到“结束”打印,但是当我检查 mongo 时,我意识到我的数组仍然只有 1 个值并且它没有更新。

我尝试使用 $set 创建新数组并更新文档,但同样的问题将其分解

没有错误,怎么了?

PS 所有其他 mongodb 方法适用于除此数组字段之外的所有文档

==========

更新:

我有 db “chanlist”和 collection “channels”与文档:

{
                "name": "FilmNews",
                "chat_link": "filmnewsss",
                "chat_id": 1283929492,
                "repost_ids": [1395174524],
}

我需要在"repost_ids"中添加1351487118 ,所以我的步骤是:

# db = chanlist db

x = db.channels.find_one({"name": "FilmNews"})

db.channels.update_one(x,  {"$push": {"repost_ids": 1351487118 } })

db.close()

print("check")

我做了这一切,在我的控制台中看到“检查”打印,转到 cloud.mongodb.com,更新页面,我的文档仍然是:

{
                    "name": "FilmNews",
                    "chat_link": "filmnewsss",
                    "chat_id": 1283929492,
                    "repost_ids": [1395174524],
}

它没有更新...

我按照您的所有步骤操作, update_one在 Python 和 mongo shell 中都可以正常工作。 我在下面包括 shell 示例:

> db.test.insert({name: "FilmNews",  "chat_link" : "filmnewsss",  "chat_id": 1283929492,  "repost_ids": [ 1395174524 ]})
WriteResult({ "nInserted" : 1 })
> db.test.findOne({name: "FilmNews"})
{
    "_id" : ObjectId("5edec43e290fc9eea2bb5e4a"),
    "name" : "FilmNews",
    "chat_link" : "filmnewsss",
    "chat_id" : 1283929492,
    "repost_ids" : [
        1395174524
    ]
}
> x=db.test.findOne({name: "FilmNews"})
{
    "_id" : ObjectId("5edec43e290fc9eea2bb5e4a"),
    "name" : "FilmNews",
    "chat_link" : "filmnewsss",
    "chat_id" : 1283929492,
    "repost_ids" : [
        1395174524
    ]
}
> db.test.updateOne(x,  {"$push": {array_field: 1 } })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.test.findOne({name: "FilmNews"})
{
    "_id" : ObjectId("5edec43e290fc9eea2bb5e4a"),
    "name" : "FilmNews",
    "chat_link" : "filmnewsss",
    "chat_id" : 1283929492,
    "repost_ids" : [
        1395174524
    ],
    "array_field" : [
        1
    ]
}
>

这是 shell 中的相同代码,因此它与您的示例完全匹配:

> db.test.drop()
true
> db.test.insert({name: "FilmNews",  "chat_link" : "filmnewsss",  "chat_id": 1283929492,  "repost_ids": [ 1395174524 ]})
WriteResult({ "nInserted" : 1 })
> x = db.test.findOne({name: "FilmNews"})
{
    "_id" : ObjectId("5ef1c348bfa1a5850c9399aa"),
    "name" : "FilmNews",
    "chat_link" : "filmnewsss",
    "chat_id" : 1283929492,
    "repost_ids" : [
        1395174524
    ]
}
> db.test.updateOne(x,  {"$push": {array_field: 1 }})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.test.findOne({name: "FilmNews"})
{
    "_id" : ObjectId("5ef1c348bfa1a5850c9399aa"),
    "name" : "FilmNews",
    "chat_link" : "filmnewsss",
    "chat_id" : 1283929492,
    "repost_ids" : [
        1395174524
    ],
    "array_field" : [
        1
    ]
}
>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM