Answer the question
In order to leave comments, you need to log in
Is it possible to keep 2 semi-identical documents in Mongo?
I need to be able to keep 2 semi-identical documents in Mongo. And so that when the first document updates the common fields, they are also updated in the second document.
That is, documents have unique fields (name, _id), there are common fields: group, country. So. One of the options: do synchronization by group. It is the same for both documents and it never needs to be changed. And, if I want to change the country for both documents, I just update by group.
Example:
// doc 1
{
_id: ObjectId(), // уникальное
gruop: "GROUP_1", // не уникальное, по этому полю будет синхронизация
name: "NameOne", // уникальное, НЕ должно изменяться вместе
country: "TEST" // не уникальное, ДОЛЖНО синхронизироваться со вторым документом
}
// doc 2
{
_id: ObjectId(), // уникальное
gruop: "GROUP_1", // не уникальное, по этому полю будет синхронизация
name: "NameTWO", // уникальное, НЕ должно изменяться вместе
country: "TEST" // не уникальное, ДОЛЖНО синхронизироваться с первым документом
}
Answer the question
In order to leave comments, you need to log in
This is how it gets updated.
https://docs.mongodb.com/manual/reference/method/d...
In the query parameter, you specify the condition for which fields and how the selection is made, then in the update parameter - what and how to modify. Do not forget that if you are not updating all the fields, then the update must be done through $set, otherwise it will delete all other fields.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question