MongoError: cannot infer query fields to set, path 'users' is matched twice
I use this as the condition
{ "users": { $all: [ {"$elemMatch": userId1}, {"$elemMatch": userId2} ] }......}
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.
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.