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

守給你的承諾、 提交于 2020-05-26 11:43:35

问题


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

This is how I do:

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

But I got the error:

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

This is Chat Schema:

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

How can I do it correctly? Thanks


回答1:


I use this as the condition

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



回答2:


There is a workaround for this issue:

db.foo.update({a:{$all:[{$elemMatch:{$eq:0}},{$elemMatch:{$eq:1}}]}},{$set:{b:1}},{upsert:true})

This will match when a is an array with both 0 and 1 in it and it will upsert otherwise.

From: https://jira.mongodb.org/browse/SERVER-13843?focusedCommentId=2305903&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-2305903




回答3:


I know this already has an answer but to hopefully save someone else some time, I had to do this:

{
  "users": {
        $all: [
          { $elemMatch: { $eq: mongoose.Types.ObjectId(userId1) }},
          { $elemMatch: { $eq: mongoose.Types.ObjectId(userId2) }}
        ]
  }......
}

Modifications from accepted answer:

  • The $eq was needed just like Dave Howson said in his comment on the accepted answer.
  • mongoose.Types.ObjectId was needed because I guess the _id property on my schema instance was a string.


来源:https://stackoverflow.com/questions/39060861/mongoerror-cannot-infer-query-fields-to-set-path-users-is-matched-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!