Answer the question
In order to leave comments, you need to log in
How to update an object in an array in an object or insert it if it's not there?
There is a DB parents-children.
Parents' names are unique. The names of children are unique to the parent.
{
parent: "Ivan Ivanov",
children: [{
{name: Masha,
age: 20,
sex: female
},
{name: Vasya,
age: 23,
sex: female
}]
}
db.parents.update({parent: "Ivan Ivanov"}, {$addToSet: {children: {name: Vasya, age: 23, sex: male}}}, {upsert:true});
Answer the question
In order to leave comments, you need to log in
https://docs.mongodb.com/manual/reference/method/d...
upsert: true
upd:
db.parents.update(
{"$and": [
{
parent: "Ivan Ivanov"
},
{
"children.name": Vasya
"children.age": 23
"children.sex": "male"
}
]},
{
"children.sex": "female"
// тут, если не сработает, попробуйте полностью обьект заменить
}
)
db.getCollection('parents').update({"parent": "Ivan Ivanov", "children.age": 23}, {"$set": {"children.$.sex": "male"}})
The names of children are unique to the parent.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question