这是我拥有的对象:
{
"_id": ObjectId("590985b1859aefea4a39982f"),
"comment": [{
"created_at": 1493880257678.0,
"comment": "12345",
"user_id": ObjectId("58edd98d40627c492c8689c5"),
"_id": ObjectId("590acdc1141b16c6521b9140"),
"modified": 1493880257678.0,
"assigned_to": null
}, {
"created_at": 1493880257678.0,
"comment": "12345",
"user_id": ObjectId("5906c50c1be98711121edf2b"),
"_id": ObjectId("590acdc1141b16c6521b9140"),
"modified": 1493880257678.0,
"assigned_to": null
}, {
"created_at": 1493880257678.0,
"comment": "12345",
"user_id": ObjectId("58edd98d40627c492c8689c5"),
"_id": ObjectId("590acdc1141b16c6521b9140"),
"modified": 1493880257678.0,
"assigned_to": null
}]
}
我希望在user_id(58edd98d40627c492c8689c5)
答案 0 :(得分:1)
您可以将findOneAndUpdate与$pull一起使用。
你将像这样构建你的查询。
findOneAndUpdate(
{_id: <id of the document>},
{$pull: {comment: {user_id: <user_id>}}},
{multi:true}
)
不确定您使用的是哪个客户端或驱动程序,但这是我在robomongo上执行的示例测试,集合名称为&#34;文档&#34;
db.getCollection('documents').findOneAndUpdate(
{_id: new ObjectId("590985b1859aefea4a39982f")},
{$pull:
{comment:{user_id : new ObjectId("58edd98d40627c492c8689c5")}}
},
multi:true})