I have a collection "user" with documents like
{
_id:ObjectId("xx"),
searches:[
{someId:"yyy","fav_food":"pasta"},
{someId: "zzz","fav_food":"macncheese"}
]
}
The someId maps to another collection "job"
{
_id:yyy,
job_name:"clerk",
"name": "kent"
},
{
_id:zzz,
job_name:"racer",
"name":"michael"
}
I have to enhance data in user collection from the job collection
So the user document should be:
{
_id:ObjectId("xx"),
searches:[
{someId:"clerk::kent",fav_food:"pasta"},
{someId: "michael::racer","fav_food":"macncheese"}
]
}
I have
mongo_db.collection('job', function(err,coll){
for(var i = 0; i <= data.searches.length-1; i++) {
var pid = data.searches[i].someId;
console.log("RELEASE ID " + someId);
if(pid !== null || pid !== undefined){
result = coll.findOne({"_id":ObjectId(pid)});
if(result){
console.log("this is data searches for index " + i+ " " + JSON.stringify(data.searches[i])
+ " and data.searches " + JSON.stringify(data.searches) + " and this is result " + JSON.stringify(result));
data.searches[i].someId = result.name + "::" + result.job_name;
}
}
}
return data;
})
This does not seem to work... any Idea how I can do this? I know I have to use Promises/Async functions but I cannot seem to find the right combination.