如何通过通用 _id 字段过滤我的事件数组?

How can I filter my events array by common _id field?

我有这样的输出,但我想按与父对象相同的 _id 过滤事件数组_id.I想根据此条件过滤它。

[
    {
        "_id": "5cc45eb430aaba3cdc045dc0" ,
        "word": "Pasta",
        "translate": "Makarna",
        "kind": "İsim",
        "exampleSentence": "asljdalsd",
        "__v": 0,
        "events": [
            {
                "_id": "5cc45eb430aaba3cdc045dc0",
                "createdAt": "2019-04-27T13:52:15.721Z",
                "TenMinutesLater": "2019-04-27T13:52:25.721Z",
                "OneWeekLater": "2019-05-04T13:52:15.721Z",
                "OneMonthLater": "2019-05-27T13:52:15.721Z",
                "FourMonthLater": "2019-08-27T13:52:15.721Z",
                "__v": 0
            },
            {
                "_id": "5cc45ee630aaba3cdc045dc1",
                "createdAt": "2019-04-27T13:52:15.721Z",
                "TenMinutesLater": "2019-04-27T13:52:25.721Z",
                "OneWeekLater": "2019-05-04T13:52:15.721Z",
                "OneMonthLater": "2019-05-27T13:52:15.721Z",
                "FourMonthLater": "2019-08-27T13:52:15.721Z",
                "__v": 0
            }
        ]
    }
]

我想过滤我的事件数组,它与父对象具有相同的 _id 属性 _id.How 应该是我根据我想要的查询?

这是我的查询

Word.find({ _id: req.params.id }, (err, words) => {
    if (err) {
      console.log(err);
    }
    const uid = words.map(word => word._id);
    console.log(req.params.id);
    Word.aggregate([
      {
        $match: {
          _id: {
            $in: uid.map(function(id) {
              return new mongoose.Types.ObjectId(id);
            })
          }
        }
      },
      {
        $lookup: {
          from: "tests",
          localField: "eventId",
          foreignField: "_id.str",
          as: "events"
        }
      }
    ])
      .then(data => {
        res.json(data);
      })
      .catch(err => {
        throw err;
      });
  });

我想要这样的输出 this.How 我用父对象的 _id 过滤它?

[
    {
        "_id": "5cc45eb430aaba3cdc045dc0" ,
        "word": "Pasta",
        "translate": "Makarna",
        "kind": "İsim",
        "exampleSentence": "asljdalsd",
        "__v": 0,
        "events": [
            {
                "_id": "5cc45eb430aaba3cdc045dc0",
                "createdAt": "2019-04-27T13:52:15.721Z",
                "TenMinutesLater": "2019-04-27T13:52:25.721Z",
                "OneWeekLater": "2019-05-04T13:52:15.721Z",
                "OneMonthLater": "2019-05-27T13:52:15.721Z",
                "FourMonthLater": "2019-08-27T13:52:15.721Z",
                "__v": 0
        ]
    }
]

此处 $lookup 将 return 所有匹配 localfield/foreign 字段条件的事件,您可能已经知道。如果您想按其他一些标准(从您的描述中不是很清楚)过滤结果,您可以在连接集合中使用管道功能(在 3.6 中引入)。 $lookup 中的示例管道(取自官方网站)如下所示。因此,如您所见,您可以对加入的集合执行匹配并过滤您的事件。

db.orders.aggregate([
   {
      $lookup:
         {
           from: "warehous`enter code here`es",
           let: { order_item: "$item", order_qty: "$ordered" },
           pipeline: [
              { $match:
                 { $expr:
                    { $and:
                       [
                         { $eq: [ "$stock_item",  "$$order_item" ] },
                         { $gte: [ "$instock", "$$order_qty" ] }
                       ]
                    }
                 }
              },
              { $project: { stock_item: 0, _id: 0 } }
           ],
           as: "stockdata"
         }
    }
])

谢谢回答,但我终于解决了这个问题,就像 this.it 工作得很好

Word.find({ _id: req.params.id }, (err, words) => {
    if (err) {
      console.log(err);
    }
    const uid = words.map(word => word._id);
    Word.aggregate([
      {
        $match: {
          _id: {
            $in: uid.map(function(id) {
              return mongoose.Types.ObjectId(id);
            })
          }
        }
      },
      {
        $lookup: {
          from: "tests",
          localField: "_id.str",
          foreignField: "eventId",
          as: "events"
        }
      },
      {
        $addFields: {
          events: {
            $filter: {
              input: "$events",
              as: "event",
              cond: {
                $eq: ["$$event._id", mongoose.Types.ObjectId(req.params.id)]
              }
            }
          }
        }
      }
    ])
      .then(data => {
        res.json(data);
      })
      .catch(err => {
        throw err;
      });
  });