MongoError:无法推断要设置的查询字段,路径“用户"被匹配两次 [英] MongoError: cannot infer query fields to set, path 'users' is matched twice

查看:93
本文介绍了MongoError:无法推断要设置的查询字段,路径“用户"被匹配两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用猫鼬.我想创建一个数组为users(包括userId1userId2)的文档chat,如果找不到的话:

I am using mongoose. I want to create a document chat with an array users (including userId1, userId2), if I do not find it:

这是我的工作方式:

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

但是我得到了错误:

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

MongoError: cannot infer query fields to set, path 'users' is matched twice

这是Chat架构:

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

如何正确执行?谢谢

推荐答案

我以此为条件

{
  "users": {
        $all: [
          {"$elemMatch": userId1},
          {"$elemMatch": userId2}
        ]
  }......
}

这篇关于MongoError:无法推断要设置的查询字段,路径“用户"被匹配两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆