mongodb - pymongo里,dict存储在list内,如何根据key-value值删除或更新该dict?
阿神
阿神 2017-04-21 11:17:36
0
2
743
{
    "title": "hello world",
    "comments": [
                    {"id": 1, "text": "text 1"},
                    {"id": 2, "text": "text 2"},
                    {"id": 3, "text": "text 3"}, 
                    ...
                ],
    "author": "",
    "views": 1000,
}

假设我现在知道某个 comment 的 id=2,想删除 comments 中 id=2 的字典,也就是 {"id": 2, "text": "text 2"} 这条数据,应该怎样写呢?

如果是更新 id=2text 的内容,又该如何写呢?

阿神
阿神

闭关修行中......

reply all(2)
Ty80

This can use mongodb’s array locator, details:
http://www.co-ding.com/?p=274

刘奇

You can delete it from the mongodb command line like this (pymongo basically has the same format, not tested):

db.posts.update({'_id': xxxx}, {'$pull': { 'comments': {'id': 2} }})

Updated, it seems that it is not so straightforward to write, so let’s go through it with for. .
//Or delete it first and then add it? This order may not meet the requirements. .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template