我正在努力将聚合查询放在一起查找来自一个集合的数据并拉入另一个集合中 . 这是我的设置......

Collection One :training_documents

数据结构示例:

{
  "_id": ObjectId("5a0350ad7df0977d94cffab6"),
  "doc_description": "Document example used for question.",
  "doc_title": "Test Document",
  "doc_id": "0000-00.00",
  "date_added": ISODate("2017-11-08T18:45:01.368Z"),
  "status": "Active",
  "__v": 0,
  "doc_category_id": ObjectId("5997074fd408ba07d7e6d3fa"),
  "doc_type_id": ObjectId("59f0982c7df0977d94cffa08"),
  "doc_site": [
    {
      "site_id": "site1",
      "site_name": "Site One",
      "active": true
    },
    {
      "site_id": "site2",
      "site_name": "Site Two",
      "active": true
    },
    {
      "site_id": "site3",
      "site_name": "Site Three",
      "active": true
    }
  ],
  "modified_by": "John Smith"
}

Collection Two :用户

数据结构示例:

{
  "_id": ObjectId("5ad782283c55b056bcc39e30"),
  "site": {
    "site_id": "site2",
    "site_name": "Site Two"
  },
  "user_name": "jsmith",
  "first_name": "John",
  "middle_name": "A",
  "last_name": "Smith",
  "full_name": "John Smith",
  "title": "This is my title",
  "email": "jsmith@example.com",
  "last_login": ISODate("2018-04-26T19:21:14.581Z"),
  "type": "full-time",
  "active": true,
  "__v": 0,
  "badge_id": "0123456789",
  "trainings": [
    {
      "document": "5a0350ad7df0977d94cffab6",
      "trainee": "5ad782283c55b056bcc39e30",
      "trainer": "595fcc2e04cf707693257890",
      "completion_time": "2018-04-23T17:40:41.198Z"
    },
    {
      "document": "5970af02d94981372d1c425c",
      "trainee": "5ad782283c55b056bcc39e30",
      "trainer": "5970b2d9d94981372d1c426c",
      "completion_time": "2018-04-23T17:40:41.198Z"
    }
  ]
}

我需要使用 Users 模型执行聚合并从 training_documents 集合中查找,其中 localField 是users trainings 数组中的每个 document 键 . foreignField 显然是来自 training_documents 集合的 _id .

最终目标是让每个受训者都拥有他们已经过培训的文档详细信息以及与他们的帐户相关联的培训师 .

我've setup the basic query but it'没有返回我的 trainings 数组中的任何数据 . 我怀疑我的 localField 有问题,因为每个 document 键应该有多个对象迭代 .

谁能帮我理解我做错了什么?

.get((req, res) => {
    UserLdap.aggregate([
        { $unwind: '$trainings' },
        { $lookup: {
            from: 'training_documents',
            localField: 'trainings.document',
            foreignField: '_id',
            as: 'trainings'
            },
        },
    ]).exec((err, results) => {
        if (err) {
            console.log(err);
        }
        else {
            res.status(200).json(results);
            console.log(results);
        }
    })