简体   繁体   English

mongodb(聚合) - 查找结果的 $lookup

[英]mongodb (aggregation) - $lookup with the lookup result

i am new in aggregation framework and have the following problem/question.我是聚合框架的新手,有以下问题。

My collections looks like this:我的 collections 看起来像这样:

users用户

    _id: ObjectId('604caef28aa89e769585f0c4')
    baseData: { 
       firstName: "max"
       username: "max_muster"
       ...
       ...
    }

activitiesFeed活动饲料

    _id: ObjectId('604cb97c99bbd77b54047370')
    userID: "604caef28aa89e769585f0c4"     // the user id 
    activity: "604caf718aa89e769585f0c8"   // the activity id 
    viewed: false 
    

activities活动

  _id: ObjectId('604caf718aa89e769585f0c8')
 "baseData": {
      "userID":"604caef28aa89e769585f0c4",
      "type":0,
      "title":"Alessandro send you a friend request.",
      "description":"",
      "actionID":"604caef28aa89e769585f0c4"
   },

aggregate function聚合 function

const response = ActivityFeed.aggregate([
      {
        $match: { userID: ObjectId(args.user) },
      },
      {
        $lookup: {
          from: 'activities',
          localField: 'activity',
          foreignField: '_id',
          as: 'lookup_activities',
        },
      },
      { $unwind: '$activities' },
      {
        $lookup: {
          from: 'users',
          localField: 'lookup_activities.baseData.actionID',
          foreignField: '_id',
          as: 'lookup_users',
        },
      },
    ]);

How i can made a lookup with the first lookup result?我如何使用第一个查找结果进行查找?

I made a lookup to the activities collection.我查找了活动集合。 This collection holds the id for the second lookup baseData.userID.此集合保存第二个查找 baseData.userID 的 id。 But with this approach i have no result with the second lookup.但是使用这种方法,我在第二次查找中没有结果。

The reason why i made a join to the activities collection is that the actionID can hold a userID or a document id from another collection.我加入活动集合的原因是 actionID 可以保存来自另一个集合的用户 ID 或文档 ID。 It depend on the type in the activities collection.它取决于活动集合中的类型。

Is it in the aggregation framework possible to made a lookup which depends on the type and of the previous lookup?是否可以在聚合框架中进行取决于类型和先前查找的查找?

Thank you in advance.先感谢您。

All I did is fixing your aggregation.我所做的只是修复你的聚合。 You unwind the wrong field and please make sure all your ID fields are ObjectId.您展开错误的字段,请确保您的所有 ID 字段都是 ObjectId。 I highly recommend using MongoDB Compass to assist your aggregation if you are new to the aggregation function.如果您不熟悉聚合 function,我强烈建议您使用MongoDB Compass来帮助您进行聚合。 They have stage by stage assistance GUI which will really make your life easier.他们有分阶段的辅助 GUI,这将真正让您的生活更轻松。

mongoplayground mongoplayground

db.activitiesFeed.aggregate([
  {
    $match: {
      userID: ObjectId("604caef28aa89e769585f0c4")
    },
  },
  {
    $lookup: {
      from: "activities",
      localField: "activity",
      foreignField: "_id",
      as: "lookup_activities",
    },
  },
  {
    $unwind: "$lookup_activities"
  },
  {
    $lookup: {
      from: "users",
      localField: "lookup_activities.baseData.actionID",
      foreignField: "_id",
      as: "lookup_users",
    },
  },
])

Sample Used使用的样品

 db={
  "users": [
    {
      _id: ObjectId("604caef28aa89e769585f0c4"),
      baseData: {
        firstName: "max",
        username: "max_muster"
      }
    }
  ],
  "activitiesFeed": [
    {
      _id: ObjectId("604cb97c99bbd77b54047370"),
      userID: ObjectId("604caef28aa89e769585f0c4"),
      activity: ObjectId("604caf718aa89e769585f0c8"),
      viewed: false
    }
  ],
  "activities": [
    {
      _id: ObjectId("604caf718aa89e769585f0c8"),
      "baseData": {
        "userID": ObjectId("604caef28aa89e769585f0c4"),
        "type": 0,
        "title": "Alessandro send you a friend request.",
        "description": "",
        "actionID": ObjectId("604caef28aa89e769585f0c4")
      },
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM