I have millions of documents in various collection.
My application has to periodically update some incremental data to MongoDB. How do I acheive this?
For every record in incremental data, if the record exists it must be updated with new values otherwise it has to be inserted.
{
_id: 'some ID',
lifnr: 12345,
bukrs: 3455,
amount: 500
},
{
_id: 'some ID',
lifnr: 12346,
bukrs: 3456,
amount: 5200
}
Assuming above is my data, with lifnr and bukrs as compound index with unique constraint. Any new records with {lifnr & bukrs} value same as any of existing should replace the old amount. If it is a new record of unique {lifnr & bukrs}, then it must insert/append it.
Guide me to the approach. Thanks
Based on your edit, its not really incrementing data. Its just an update with upsert set to true. Do update every time. The query ill be,
db.collection.update(
{ lifnr: 12345, bukrs: 3455},
{$set: {amount: 5600}}
{upsert: true}
)
The above query would update amount, if the linfr & bukrs combination is found. If not found it will insert a new document.