Reputation: 693
I'm a MongoDB newbie, but have gone through some basic trainings. However, I'm not able to handle this (seemingly simple) situation: I'm populating my database with results from two API calls. The first call returns a bunch of fields that include a primary key: NAME. The second call gets additional details for each NAME. I've inserted the results of the first call into the database. When I do the second call, I want to update the document with the matching NAME to include all the additional fields and values returned by the second API call. (ex. the first call returns a recipe NAME and a bunch of metadata about the recipe; the second call returns a list of ingredients, and I want to add those to the document for that recipe based on the matching NAME).
Shouldn't this be easy? Or does this constitute a merge, which I believe mongo does not support? In that case I assume I just insert the second call's results as a separate document and do future queries on NAME in order to pull both documents relating to that NAME?
Upvotes: 0
Views: 200
Reputation: 14389
You have to use db.collection.update()
call to get it done. What you do in this is that you pass in the query to locate the right document, in your case it is NAME, and then update the document appropriately.
Ex: Let's say the NAME be updated is 'eamcvey' and you want to put in Address and Phone Number field. The command you would type in for that would look like:
db.collection.update(
{NAME : 'eamcvey'},
{
$set : {Address : 'Updated Value of Address'},
$set : {PhoneNumber : '123456'}
},
);
For more detailed documentation, go to this link on update command in MongoDB.
Upvotes: 2