简体   繁体   English

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

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

I have the document with array field and 1st value in it.我有包含数组字段和第一个值的文档。

I want to update this document and add the value in array, so I get the document by find_one command and use this code:我想更新这个文档并在数组中添加值,所以我通过 find_one 命令获取文档并使用以下代码:

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

So, I can see the "end" print, but when I check mongo I realize that my array still has only 1 value and it wasn't update.所以,我可以看到“结束”打印,但是当我检查 mongo 时,我意识到我的数组仍然只有 1 个值并且它没有更新。

I tried to make the new array and update document with $set, but same problem break it up我尝试使用 $set 创建新数组并更新文档,但同样的问题将其分解

No errors, what's wrong?没有错误,怎么了?

PS all other mongodb methods works fine with all documents except of this array-field PS 所有其他 mongodb 方法适用于除此数组字段之外的所有文档

========== ==========

Update:更新:

I have db "chanlist" and collection "channels" with document:我有 db “chanlist”和 collection “channels”与文档:

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

I need to add 1351487118 in "repost_ids" , so my steps are:我需要在"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")

I did it all, seeing "check" print in my console, going to cloud.mongodb.com, updating page and my document still is:我做了这一切,在我的控制台中看到“检查”打印,转到 cloud.mongodb.com,更新页面,我的文档仍然是:

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

Its not updated...它没有更新...

I followed all your steps and the update_one worked just fine for my in both Python and the mongo shell.我按照您的所有步骤操作, update_one在 Python 和 mongo shell 中都可以正常工作。 I include the shell example below:我在下面包括 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
    ]
}
>

Here is the same code in the shell so it matches your example exactly:这是 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.

相关问题 忽略Python中的XML错误 - Ignoring XML errors in Python 忽略命令中的异常无:discord.ext.commands.errors.CommandNotFound:找不到命令“播放”。 这发生在 python - Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "play" is not found. this happens in python python Discord bot 出现问题(discord.ext.commands.bot:忽略命令中的异常 None discord.ext.commands.errors.CommandNotFound) - Trouble with python Discord bot (discord.ext.commands.bot: Ignoring exception in command None discord.ext.commands.errors.CommandNotFound) 命令提示符中出现 Python 错误 - Python errors in command prompt python无法将数据推送到mongoDB - python not able to push data to mongoDB Python忽略命令从列表中删除项目 - Python ignoring command to remove items from list 使用Push-to-Deploy for Python遇到无法解释的错误 - Experiencing Unexplained Errors with Push-to-Deploy for Python 我如何在不重启机器人的情况下停止这个 discord 机器人命令? - How i can stop this discord bot command wihout restart the bot? '忽略命令中的异常无:discord.ext.commands.errors.CommandNotFound:未找到命令“测试”'Discord py中的错误 - 'Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "test" is not found' error in Discord py 如何使用python将csv数据推送到mongodb - how to push a csv data to mongodb using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM