I have a structure like this:
{
...
_id: <projectId>
en-GB: [{
_id: <entryId>,
key: 'some key',
value: 'some value',
}]
}
And I've tried updating it with Mongoose (and raw mongodb too) like this:
const result = await Project
.update({
_id: projectId,
'en-GB._id': entryId,
}, {
$set: {
'en-GB.$.key': 'g000gle!!',
},
})
.exec();
I've checked that the IDs are correct. But it doesn't update anything:
{ n: 0, nModified: 0, ok: 1 }
What am I doing wrong? Thanks
n: 0). I see nothing wrong with your query itself, so I would recommend ensuring that the ids both on the document and in your query are of the correct type (e.g. make sure you're not comparing strings to ObjectIds). I would also recommend attempting a simple find on your fields individually and then together to help isolate the problem.findByIdAndUpdatewhich take an id string, but methods that don't explicitly operate on ids typically expect your id strings to be wrapped within an ObjectId.findByIdAndUpdateonly take an id string as one parameter. When you're associating an id with a document field (e.g.{$set: {key: some_id}}), you must always, always use an ObjectId. This is true for every part of a query, from the match to the update. String representations of ids are a very special case and should always be treated as the exception to the rule.Stringseems to work on_id, but failed on'en-GB._id'. Feel free to add it as an answer