{ "title": "hello world", "comments": [
{"id": 1, "text": "text 1"},
{"id": 2, "text": "text 2"},
{"id": 3, "text": "text 3"},
...
], "author": "", "views": 1000,
}假設我現(xiàn)在知道某個 comment 的 id=2,想刪除 comments 中 id=2 的字典,也就是 {"id": 2, "text": "text 2"} 這條數據,應該怎樣寫呢?
1 回答

冉冉說
TA貢獻1877條經驗 獲得超1個贊
從mongodb命令行可以這樣刪除(pymongo基本上格式一樣了,沒測試):
db.posts.update({'_id': xxxx}, {'$pull': { 'comments': {'id': 2} }})
更新的話 寫起來貌似就沒有這么直白了,for遍歷一下吧。
//或者是先刪了再添加進去?這樣順序可能不滿足要求了。
- 1 回答
- 0 關注
- 196 瀏覽
添加回答
舉報
0/150
提交
取消