Node.js mongodb中的替代填充?

Node.js mongodb中的替代填充?,node.js,mongodb,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongodb Query,Aggregation Framework,我有一个名为group的数组,其中包含一个ID列表,我有下面的find和populate语句,它与此完美配合,我使用populate来获取其他数据-下面的查询: var stories = await Story .find( { 'group' : { $in : group } } ) .populate('group') .populate('createdBy') .sort('-created') 我有一个聚合查询(如下所示),它实现了我想要的功能,但1)它不使用组数

我有一个名为
group
的数组,其中包含一个ID列表,我有下面的find和populate语句,它与此完美配合,我使用populate来获取其他数据-下面的查询:

var stories = await Story
  .find( { 'group' : { $in : group } } )
  .populate('group')
  .populate('createdBy')
  .sort('-created')
我有一个聚合查询(如下所示),它实现了我想要的功能,但1)它不使用
数组中的值,它只返回所有内容,2)我不获取
createdBy
字段的附加数据,就像我对上面的
查找
字段所做的那样

var spellings = await Spelling.aggregate([
  { "$sort": { "keyStageLevel": 1, "spellingLevel": 1 } },
  { "$group" : {
    "_id": { "keyStageLevel": "$keyStageLevel", "class": "$class" },
    "data": {
      "$push": {
        "spellingLevel": "$spellingLevel",
        "class": "$class",
        "name": "$name",
        "spellings": "$spellings",
        "created": "$created",
        "_id": "$_id",
        "group": "$group",
        "createdBy": "$createdBy"
      }
    }
  }},
  { "$sort": { "_id": 1 } }
])
编辑-以下是拼写文档示例:

 {
    "_id":"5b1ff6f62bb1894efcf76ea0",
    "spellings":["here","are","spellings"],
    "name":"withclass",
    "group":"5ab79a639083cf35339b880a",
    "createdBy":"5ab79185d47b833506ff94b1",
    "created":"2018-06-12T16:38:14.571Z",
 }
有谁能帮助我在我的
聚合
语句中如何使用
数组中的值,以及如何为
创建的
字段添加附加数据,就像我为
查找
所做的那样?

您可以尝试在聚合下面填充
createdBy
字段

var spellings = await Spelling.aggregate([
  { "$match": { "group": { "$in": group }}},
  { "$sort": { "keyStageLevel": 1, "spellingLevel": 1 } },
  { "$lookup": {
    "from": CreatedBy.collection.name,
    "let": { "createdBy": "$createdBy" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$createdBy" ] } } }
    ],
    "as": "createdBy"
  }},
  { "$lookup": {
    "from": Group.collection.name,
    "let": { "group": "$group" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$group" ] } } }
    ],
    "as": "group"
  }},
  { "$unwind": "$group" },
  { "$unwind": "$createdBy" },
  { "$group" : {
    "_id": { "keyStageLevel": "$keyStageLevel", "class": "$class" },
    "data": {
      "$push": {
        "spellingLevel": "$spellingLevel",
        "class": "$class",
        "name": "$name",
        "spellings": "$spellings",
        "created": "$created",
        "_id": "$_id",
        "group": "$group",
        "createdBy": "$createdBy"
      }
    }
  }},
  { "$sort": { "_id": 1 } }
])

您需要使用
$lookup
聚合阶段,该阶段可以轻松填充您的
createdBy
。。。你有哪个mongodb版本?谢谢@Ashish,我在V3.6.3上