mongodb的联合查询

db.assets.aggregate([
{$match:{"stationId":"HNRDC"}},
{$match:{"categoryType":"TYPE_FOLDER_COMMON"}},
{$match:{"extraData.folder_used":{$gte:0}}},{
$lookup: {
  from: "departs", 
  localField: "parentId", 
  foreignField: "_id", 
  as: "group"
}
},
{
$unwind: {
  path: "$group",
  preserveNullAndEmptyArrays: false 
}
},

{
      $project: {
       name:1,
       stationId:1,
       categoryType:1,
       "extraData.folder_used":1
       }
},

]);
db.groups.aggregate([
{$match:{"stationId":"HNRDC"}},
{
$lookup: {
  from: "mobjects", 
  localField: "_id", 
  foreignField: "parentId", 
  as: "mos"
}
},

{
      $project: {
      name:1,
      stationId:1,
      mos: {
      $filter: {
            input: "$mos",
               as: "item",
               cond: { $eq: [ "$$item.categoryType", "TYPE_FOLDER_COMMON" ] }
            }
         }
      }
},
{
$unwind: {
  path: "$mos",
  preserveNullAndEmptyArrays: false 
}
},

]);

 

posted @ 2021-03-26 14:40  凉城  阅读(463)  评论(0编辑  收藏  举报