U
U
Urukhayy2017-08-28 14:03:57
MongoDB
Urukhayy, 2017-08-28 14:03:57

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" // не уникальное, ДОЛЖНО синхронизироваться с первым документом

}

If I want to update the country of all documents whose group is equal to "GROUP_1", then I write the usual update({group: "GROUP_1"}). Will it be possible to sync like this? Or are there other ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Ptolemy_master, 2017-08-29
@Ptolemy_master

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 question

Ask a Question

731 491 924 answers to any question