Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Mongo-基于用户名从一个集合中的多个数组中删除多个元素_Arrays_Mongodb_Mlab - Fatal编程技术网

Arrays Mongo-基于用户名从一个集合中的多个数组中删除多个元素

Arrays Mongo-基于用户名从一个集合中的多个数组中删除多个元素,arrays,mongodb,mlab,Arrays,Mongodb,Mlab,我收集了一组诈骗电话号码,其中包含来自不同用户的评论。每个用户都有一个唯一的显示名称。我正在尝试删除所有特定于该用户名的注释。到目前为止,我可以通过以下方式找到包含该特定用户名注释的文档: db.collection.find({"comments":{$elemMatch:{creator:"name"}}}) 我只想删除所有帖子的用户评论,而不是帖子本身。我觉得我很接近,但找不到一个 查找结果: { "_id" : ObjectId("5b84a319ec18e50d9093f3aa"),

我收集了一组诈骗电话号码,其中包含来自不同用户的评论。每个用户都有一个唯一的显示名称。我正在尝试删除所有特定于该用户名的注释。到目前为止,我可以通过以下方式找到包含该特定用户名注释的文档:

db.collection.find({"comments":{$elemMatch:{creator:"name"}}})
我只想删除所有帖子的用户评论,而不是帖子本身。我觉得我很接近,但找不到一个

查找结果:

{ "_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 }

可以使用update操作符删除与特定查询匹配的数组元素。就你而言:

db.collection.updateMany(
    {"comments":{$elemMatch:{creator:"name"}}}, // original query
    {
      $pull: {
        comments: {
          creator: "name"
        }
      }
    })

可以使用update操作符删除与特定查询匹配的数组元素。就你而言:

db.collection.updateMany(
    {"comments":{$elemMatch:{creator:"name"}}}, // original query
    {
      $pull: {
        comments: {
          creator: "name"
        }
      }
    })

使用
db.colname.update({},{$pull:{“comments”:{“creator”:name}},{multi:true})
db.colname.update({},{$pull:{“comments”:{“creator”:name}},{multi:true})是否有办法将
$pull
$slice
一起使用,以便从数组中删除一系列项?例如,“从索引为0-2的数组中提取项”将删除3项。@user1063287您可以使用MongoDB 4.2使用
$set
$slice
更新管道,如中所示,是否有方法将
$pull
$slice
一起使用,以便从数组中删除一系列项?例如,“从索引为0-2的数组中提取项”将删除3项。@user1063287您可以使用MongoDB 4.2使用
$set
$slice
更新管道,如中所示