簡體   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