Merge $lookup result to existing array

前端 未结 2 672
轮回少年
轮回少年 2021-01-20 17:26

I\'m new to mongo and I need your help.

I have collection studijneProgramy. This is sample document:

{
    \"_id\" : \"dGFY\",
    \         


        
2条回答
  •  暖寄归人
    2021-01-20 18:03

    Other than the potential duplicate pointed out by dege, you can use JS as follows if it isn't a lot of data. Using aggregate and $lookup would be faster, but this is cleaner, I believe.

    db.studijneProgramy.find().forEach(stu=>{
        stu.garranti=stu.garranti.map(gar=>{
            gar.garant=db.osoby.find({_id:gar.id})[0];
            return gar;
        });
        db.studijneProgramy.save(stu);
    });
    

提交回复
热议问题