如何在 Mongoose 模型中搜索模式数组索引

How to search up for an schemas array index inside a Mongoose model

我有以下文档,其中 globalData 是我的 globalEventSchema 中的一个数组。

{
  "globalData": [
    {
      "event": "event-a",
      "generatedId": "969046612024361060",
      "_id": "626a0d5553e6eb2f5e1ce714"
    },
       // More instances of the Schema
  ],
  "__v": 0
}

我想搜索我的 GlobalEvents 并找到 globalData 数组,其中 event 与我给定的字符串匹配。我怎样才能使用猫鼬实现这一点?

以下将在 globalData 数组中查找指定的 event

router.get('/', async (req, res) => {
  const { event } = req.body;
  const globalEvent = await GlobalEvent.findOne({
    'globalData.event': event,
  });
  if (globalEvent) {
    res.json(globalEvent);
  } else {
    res.json({
      error: 'No global event found',
    });
  }
});