Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
Mongodb 在文档中对数组使用updateMany和$pull会导致MongoError 11000唯一索引的重复键错误_Mongodb_Mongoose - Fatal编程技术网

Mongodb 在文档中对数组使用updateMany和$pull会导致MongoError 11000唯一索引的重复键错误

Mongodb 在文档中对数组使用updateMany和$pull会导致MongoError 11000唯一索引的重复键错误,mongodb,mongoose,Mongodb,Mongoose,我一直在想,为什么会出现以下错误: MongoError:E11000重复密钥错误集合:db_test.contentcomments索引:comments.guid_1 dup密钥:{comments.guid:null} 执行此操作时: ContentComment.updateMany( {}, { $pull: { comments: { userId: userIdToDelete } } }) 模型ContentComment: const Co

我一直在想,为什么会出现以下错误:

MongoError:E11000重复密钥错误集合:db_test.contentcomments索引:comments.guid_1 dup密钥:{comments.guid:null}

执行此操作时:

ContentComment.updateMany(
    {},
    {
      $pull: { comments: { userId: userIdToDelete } }
    })
模型
ContentComment

const Comment = new Schema({
  guid: {
    type: String,
    unique: true,
    required: true
  },
  userId: {
    type: String,
    required: true,
    unique: false,
    index: true
  },
  comment: String,
  timestamp: {
    type: Date,
    default: new Date()
  }
});

const ContentComment = new Schema({
  contentGuid: {
    type: String,
    required: true,
    unique: true,
    index: true
  },
  comments: [Comment],
  counter: {
    type: Number,
    default: 0,
  }
});
查看Robo3T中的集合,我看到有四个索引:

{
    "_id" : 1
}

对于contentGuid和guid,选中“唯一”字段

示例文档:

{
    "_id" : ObjectId("5e1c86ba03ca187cb10456e3"),
    "contentGuid" : "295582c2-a710-40ee-893a-907921dd6fcd",
    "__v" : 0,
    "comments" : [ 
        {
            "timestamp" : ISODate("2020-01-13T15:03:25.266Z"),
            "_id" : ObjectId("5e1c86bd97adbe23d00a7cs0"),
            "userId" : "f8da5e7d-8e92-4a3c-9765-f69af8bs1s81",
            "comment" : "Test",
            "guid" : "e700c572-85b7-42fc-8c9d-4d43esd322de"
        }
    ],
    "counter" : 1
}
正如您所看到的,我不想插入任何文档。我尝试将
update
multi:true
一起使用,但得到了相同的错误。我还尝试使用
find({})
获取所有文档,然后使用
forEach
迭代文档,使用
$set
为其提供一个“手动过滤”的新注释数组,但我总是会得到相同的错误

另外,我非常确定语法应该是正确的,因为我已经在不同的集合上成功地运行了它!我检查了所有的guid,它们都不相同


有没有人看到我做错了什么,或者知道如何解决这个问题?提前谢谢

我删除了guid上的索引(通过robo3t),现在它可以工作了。我注意到在模型中,guid没有索引标志:true。我不确定,为什么mongo数据库中会有索引。。也许有人可以对此发表评论。

我删除了guid上的索引(通过robo3t),现在它可以工作了。我注意到在模型中,guid没有索引标志:true。我不确定,为什么mongo数据库中会有索引。。也许有人可以对此发表评论。

请列出您在此集合上的索引,并提供文档示例。请列出您在此集合上的索引,并提供文档示例。您可能以前拥有此集合,然后将其删除。为了确保只在模型中显式定义索引,请使用此方法:您可能以前有过它,然后将其删除。要确保仅在模型中显式定义索引,请使用以下方法:
{
    "comments.guid" : 1
}
{
    "comments.userId" : 1
}
{
    "_id" : ObjectId("5e1c86ba03ca187cb10456e3"),
    "contentGuid" : "295582c2-a710-40ee-893a-907921dd6fcd",
    "__v" : 0,
    "comments" : [ 
        {
            "timestamp" : ISODate("2020-01-13T15:03:25.266Z"),
            "_id" : ObjectId("5e1c86bd97adbe23d00a7cs0"),
            "userId" : "f8da5e7d-8e92-4a3c-9765-f69af8bs1s81",
            "comment" : "Test",
            "guid" : "e700c572-85b7-42fc-8c9d-4d43esd322de"
        }
    ],
    "counter" : 1
}