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

{    "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"} 这条数据,应该怎样写呢?


郎朗坤
浏览 106回答 1
1回答

冉冉说

从mongodb命令行可以这样删除(pymongo基本上格式一样了,没测试):db.posts.update({'_id': xxxx}, {'$pull': { 'comments': {'id': 2} }})更新的话 写起来貌似就没有这么直白了,for遍历一下吧。//或者是先删了再添加进去?这样顺序可能不满足要求了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MongoDB