MongoError:无法推断要设置的查询字段,路径“用户”匹配两次

2024-03-01

我正在使用猫鼬。我想创建一个文档chat用一个数组users(包括userId1, userId2),如果我没有找到它:

我就是这样做的:

ChatModel.findOneAndUpdate(
  { users: { $all: [userId1, userId2] }},
  { $setOnInsert: {
    users: [userId1, userId2]
  }},
  { upsert: true })
  .exec()
  .catch(err => console.log(err));

但我得到了错误:

MongoError:无法推断要设置的查询字段,路径“用户”匹配 两次

This is Chat Schema:

{
  users: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  createdAt: { type: Date, default: Date.now }
}

我怎样才能正确地做到这一点?谢谢


我用这个作为条件

{
  "users": {
        $all: [
          {"$elemMatch": userId1},
          {"$elemMatch": userId2}
        ]
  }......
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MongoError:无法推断要设置的查询字段,路径“用户”匹配两次 的相关文章

随机推荐