Reputation: 117
I have a collection representing Wikipedia pages, with title, categories and links, as represented below.
{
"_id" : NumberLong(1975),
"Categories" : [
"pacific ocean"
],
"Links" : [
"volcano",
"trench",
"water",
...
"kilometer",
"plate tectonics",
"el niño"
],
"Title" : "pacific ocean"
}
I need to update some values in the Links array, to do this, I'm doing the following.
db.WikipediaArticles.update({Links:"kilometer"}, {$set: {"Links.$": "kilometre"}}, {upsert:false, multi:true})
This, however, is not updating all documents (as expected) and I end up having to run it 3-4 times to have all records updated. Is there anyway I can guarantee that all documents are updated?
Upvotes: 1
Views: 280
Reputation: 311865
$
identifies the index of the first matching element in the query, so each time you run the update
it will only modify at most one Links
element per doc.
Because Links
can contain multiple elements of "kilometer"
, you need to keep running the update
command until it reports that no docs were affected.
Upvotes: 3