G
G
Garret2014-07-10 19:22:39
MongoDB
Garret, 2014-07-10 19:22:39

How in MongoDB to update objects in an array with new keys only?

Given a collection of approximately the following documents:

{
  "book": "Dune",
  "persons": [
    {"name": "Leto", "age": 52},
    {"name": "Paul", "age": 15}
  ]
}

Is there a way to update the objects in the persons array by passing in only the new keys, as can be done with $set ? To get the output:
{
  "book": "Dune",
  "persons": [
    {"name": "Leto", "age": 52, "alive": false},
    {"name": "Paul", "age": 15, "alive": true}
  ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Krasnodemsky, 2014-07-11
@Garret

for one entry

db.books.update( { "persons.name": "Leto" }, { $set: { "persons.$.alive": true }} );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question