Reputation: 1357
I have to be missing some small detail here. My goal is to update ALL activities Not Equal to ObjectId("5e3e3c046e76599afe96d50f")
I am trying to do the following query:
db.getCollection("projects").updateMany(
{
"activities._id": { $ne: ObjectId("5e3e3c046e76599afe96d50f") }
},
{
$set: { "activities.$.totalPointsPossible": 5 }
})
and I get the following error:
WriteError({
"index" : 0,
"code" : 2,
"errmsg" : "The positional operator did not find the match needed from the query.",
"op" : {
"q" : {
"activities._id" : {
"$ne" : ObjectId("5e3e3c046e76599afe96d50f")
}
},
"u" : {
"$set" : {
"activities.$.totalPointsPossible" : 5
}
},
"multi" : true,
"upsert" : false
}
})
However, if I do this query:
db.getCollection("projects").find({ "activities._id": { $ne: ObjectId("5e3e3c046e76599afe96d50f") } })
I get the expected returned documents. I am not sure entirely how .find()
is matching the _id
fields for activities
and then the positional operator isn't matching?
Schema
{
title: {
type: String,
required: [true, "Title is required for Project"]
},
...
activities: [
{
name: {
type: String,
required: true
}
...
]
}
],
createdOn: { type: Date, default: Date.now }
}
Upvotes: 0
Views: 346
Reputation: 37048
You are missing this part from the docs:
If the query matches the array using a negation operator, such as $ne, $not, or $nin, then you cannot use the positional operator to update values from this array.
However, if the negated portion of the query is inside of an $elemMatch expression, then you can use the positional operator to update this field.
https://docs.mongodb.com/manual/reference/operator/update/positional/#negations
Upvotes: 2