我有一个骗局电话号码集合,其中包含来自不同用户的评论。每个用户都有一个唯一的显示名称。我正在尝试删除所有针对该用户名的注释。到目前为止,我可以使用以下命令找到包含来自该特定用户名的注释的文档:
{ "_id" : ObjectId("5b84a319ec18e50d9093f3aa"),
"phoneNumber" : 2334445555,
"flags" : 1,
"description" : "Charity",
"comments" : [
{ "_id" : ObjectId("5b84a319ec18e50d9093f3ab"), "content" : "Red cross asked me to donate using Moneygram", "creator" : "jv3123", "created" : ISODate("2018-08-28T01:19:21.368Z") } ], "created" : ISODate("2018-08-28T01:19:21.369Z"), "__v" : 0 }
{ "_id" : ObjectId("5b84a4e2ec18e50d9093f3ac"),
"phoneNumber" : 2334445555,
"flags" : 1,
"description" : "Charity",
"comments" : [
{ "_id" : ObjectId("5b84a4e2ec18e50d9093f3ad"), "content" : "Red cross rep asked me to send money through Moneygram", "creator" : "jv3123", "created" : ISODate("2018-08-28T01:26:58.532Z") } ], "db.phoneNumberData.find({"comments":{$elemMatch:{creator:"jv3123"}}})
{ "_id" : ObjectId("5b84a319ec18e50d9093f3aa"),
"phoneNumber" : 2334445555,
"flags" : 1,
"description" : "Charity",
"comments" : [ { "_id" : ObjectId("5b84a319ec18e50d9093f3ab"), "content" : "Red cross asked me to donate using Moneygram", "creator" : "jv3123", "created" : ISODate("2018-08-28T01:19:21.368Z") } ], "created" : ISODate("2018-08-28T01:19:21.369Z"), "__v" : 0 }
{ "_id" : ObjectId("5b84a4e2ec18e50d9093f3ac"),
"phoneNumber" : 2334445555, "flags" : 1, "description" : "Charity", "comments" : [ { "_id" : ObjectId("5b84a4e2ec18e50d9093f3ad"), "content" : "Red cross rep asked me to send money through Moneygram", "creator" : "jv3123", "created" : ISODate("2018-08-28T01:26:58.532Z") } ], "created" : ISODate("2018-08-28T01:26:58.532Z"), "__v" : 0 }
我只想删除所有帖子的用户评论,而不是帖子本身。我觉得我已经接近了,但是找不到
查找结果:
(.*)\nGeneral Information:
答案 0 :(得分:1)
您可以使用更新运算符$pull
删除与特定查询匹配的数组元素。就您而言:
db.collection.updateMany(
{"comments":{$elemMatch:{creator:"name"}}}, // original query
{
$pull: {
comments: {
creator: "name"
}
}
})