Thursday 1 June 2017

Mongodb upsert command in nodejs

I have created a collection named Account. At first shot, I am inserting around 5 records. [

{"Id":"0012800000C6Q19AAF","Name":"Aditya Kumar"}
{"Id":"0012800000C6MldAAF","Name":"GenePoint"}
{"Id":"0012800000C6MlbAAF","Name":"United Oil & Gas, UK"}
{"Id":"0012800000C6MlcAAF","Name":"United Oil & Gas, Singapore"}
{"Id":"0012800000C6MlTAAV","Name":"Edge Communications"}
{"Id":"0012800000C6MlUAAV","Name":"Burlington Textiles Corp of America"}
]

I am bringing data from the external system, which has already an ID field associated with it. Again next time when a request comes for inserting extra 5 records, I want to check if Id field of upcoming records already exists in my MongoDB(if yes, then do an update else insert). And don't get confused by Id field coming from external system with _Id field created for every MongoDB document.



via Ajeet

No comments:

Post a Comment