更新mongodb中嵌套数组中的几个字段(使用pymongo)

2024-04-30 02:08:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更新数组内数组中的一些字段

示例文档如下:

{
id: 987654321
tweets: [
    {
        text: "RT @947FreshFM: A vigil will be held for #SandyHook victims at UMd. at 7pm at Nyumburu Ampitheater. @BlackTerp",
        urls: [

        ],
        id: 279718351289348100
    },
    {
        text: "RT @WTOP: McDonnell: If you talk completely about guns, I think you're missing the point. #AskTheGov http://t.co/hbFt7t1n",
        urls: [
            {
                status: null,
                domain: null,
                url: "http://t.co/hbFt7t1n",
                period: null,
                resolved_url: null,
                annotation: null
            }
        ],
        id: 281061376275906560
    }
],

}

我想将url数组更新为:

^{pr2}$

我用这样的方法来更新:

collection.update({"id":987654321, "tweets.id":281061376275906560,"tweets.urls.url":"http://t.co/hbFt7t1n"},
{"$set":{
    "tweets.urls.resolved_url":"http://wtop.com/?nid=610&sid=3162096",
    "tweets.urls.domain": "wtop.com",
    "tweets.urls.annotation2":"guncontrol"
}}, False,False)

但是它给了我错误

can't append to array using string field name [urls]

有什么建议吗?在


Tags: textyouidhttpurldomain数组urls
1条回答
网友
1楼 · 发布于 2024-04-30 02:08:47

我不能肯定,但这可能就是发生的事情。在示例文档中,模型具有以下模式:

{  
    id: Number,  
    tweets: Array  
}

在中搜索模型实例时

^{pr2}$

很可能它找不到您要查找的模型的实例。在

我将尝试运行此脚本以查看您的搜索条件是否有效:

console.log(collection.find({
    "id": 987654321,
    "tweets.id": 281061376275906560,
    "tweets.urls.url": "http://t.co/hbFt7t1n"
}));

希望有帮助!在

相关问题 更多 >