遍历数组的 Mongo DB 查询

问题描述 投票:0回答:0

我想在 mongo shell 中运行一个 mongo 数据库查询。 它针对数组的每个元素运行,其中每个元素由 participantId 和 meal id 组成。 查询执行此操作->

mealPlanPairs = [ { "participantId": "60647d142330a251a859c300", "_id": "640891c8184c7bb950b75407" },  { "participantId": "6065885a5adde01b1ee8a85f", "_id": "640891ac184c7bb950b75406" } ]; // Loop through each pair and perform the update mealPlanPairs.forEach(function(pair) {  db.ParticipantDietMealPlanDetailsDTO.updateOne( { "participantId": pair.participantId },    { $set: { "mealPlan": db.MasterMealPlanDTO.findOne({ "_id": ObjectId(pair._id) }).mealPlan } } );});

现在在 shell 中我不能运行这个查询,因为 mongo shell 不支持这个。所以目前我正在通过手动添加 id 为每个元素运行此查询。 我如何编写一个遍历数组并执行相同操作的查询?

目前我是手动做的

db.getCollection('PARTICIPANT_DIET_MEAL_PLAN_DETAILS_PLAN').updateMany( { "participantId": "5f42698f60bb57ecc2049f8f"},  { $set: { "mealPlan": db.MASTER_MEAL_PLAN.findOne({ "_id": "64088fa3184c7bb950b753f6"}).mealPlan } }  )
有没有办法在一个 mongo 数据库查询中做到这一点。

database mongodb nosql aggregation
© www.soinside.com 2019 - 2024. All rights reserved.