Mongoose-如何从唯一数组中删除所有元素



我的架构中有一个唯一的数组AS:

requests: {
    type: [
      {
        type: Schema.Types.ObjectId,
        ref: "user",
        unique: true,
        required: false
      }
    ],
    validate: [memberLimit, "{PATH} overflow"]
  }

成员限制功能:

function memberLimit(val) {
  return val.length <= 6;
}

要从数组中删除所有元素,我尝试了:

team.requests = [];

它正在产生错误:

{ MongoError: E11000 duplicate key error index: heroku_sx6rhpl8.teams.$requests_
1 dup key: { : undefined }
    at Function.MongoError.create (H:Projectsxyznode_modulesmongoosenode_
modulesmongodb-coreliberror.js:45:10)
    at toError (H:Projectsxyznode_modulesmongoosenode_modulesmongodblib
utils.js:149:22)
    at H:Projectsxyznode_modulesmongoosenode_modulesmongodblibcollecti
on.js:1035:39
    at H:Projectsxyznode_modulesmongoosenode_modulesmongodb-corelibcon
nectionpool.js:541:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  name: 'MongoError',
  message: 'E11000 duplicate key error index: heroku_sx6rhpl8.teams.$requests_1
dup key: { : undefined }',
  driver: true,
  index: 0,
  code: 11000,
  errmsg: 'E11000 duplicate key error index: heroku_sx6rhpl8.teams.$requests_1 d
up key: { : undefined }' }

我在做什么错?

尝试确认teams.requests类型是数组。如果是这样,请尝试清除数据库中的所有索引并重新运行代码。希望有帮助。

相关内容

  • 没有找到相关文章

最新更新