Hey everyone!
I’m not very experienced with MongoDB, and I would like a suggestion.
I have a collection, it will be updated daily by automatically uploading an XLSX file in which each row represents a document of the collection and they will have some sort of unique identifier, imagine that the document “person” has the field “username” and at any given time in the whole collection there can be only one document with that username and the field “active” = true.
Imagine that the first XLSX is uploaded, the collection will be empty so all the documents will be inserted with “active” = true.
[
{
"userName": "User Name 1",
"email": "email1@email.com",
"active": true
},
{
"userName": "User Name 2",
"email": "email2@email.com",
"active": true
},
{
"userName": "User Name 3",
"email": "email3@email.com",
"active": true
}
]
Collection after first upload.
The next day a second XLSX is uploaded containing the following data:
[
{
"userName": "User Name 1",
"email": "email1@email.com",
"active": null
},
{
"userName": "User Name 2",
"email": "new_email2@email.com",
"active": null
},
{
"userName": "User Name 4",
"email": "email4@email.com",
"active": null
}
]
At this point, the first document (User 1) of the new XLSX file is identical to the existing document in the collection, so nothing needs to be done and I ignore it… The second document (User 2) changed his “email” so I need to change the “active” field from the existing document in the collection to false and insert a new document that will completely replace the existing information relative to User 2 with the new data in the XLSX file and “active” = true… The third document is new to the collection so it will be inserted normally as a new document with “active” = true.
The Resulting collection will be as follow:
[
{
"userName": "User Name 1",
"email": "email1@email.com",
"active": true
},
{
"userName": "User Name 2",
"email": "email2@email.com",
"active": false
},
{
"userName": "User Name 3",
"email": "email3@email.com",
"active": true
},
{
"userName": "User Name 2",
"email": "new_email2@email.com",
"active": true
},
{
"userName": "User Name 4",
"email": "email4@email.com",
"active": true
}
]
I cannot update the previous document if something changed, is a project requirement, so I keep a sort of “history” of every single variation.
The “real” user information for a given username will be defined by the “active” flag.
What will be the best way to do this using MongoDB? Eficiency wise.
Any ideas about how to face face this situation?
Preferably MongoDB side, not by fetching all the data and comparing all fields backend side.
Thanks in advance for the help <3