嵌套对象数组的 Mongo 文本搜索聚合

时间:2021-07-10 14:17:53

标签: node.js mongodb

我有一个包含嵌套对象数组的文档,其中包含一长串属性。我不需要搜索文档,而是需要在一个文档的嵌套数据中进行搜索。

(嵌套属性的索引类似于 'data.$**': 'text'

文档项

    "_id": "...",
    "data": [
         {
            "name": "Bob"
            ... 
         },
         {
            "name": "Mark" 
            ...
         },
         {
            "name": "John"
            ... 
         }
      ]
    }

一些错误的代码示例,用于解释我想要什么(导致错误 '...$text is only allowed...'

   const foundData = await this.fileModel
      .aggregate(
        [
          { $unwind: '$data' },
          { $match: { $text: { $search: "Mark" } } },
        ])
      .exec();

没有 $unwind 它当然可以工作,但会返回数组中的整个对象。

需要的结果

    "_id": "...",
    "data": [
         {
            "name": "Mark" 
            ...
         }
      ]
    }

P.Sthisthis 之类的解决方案不行,因为我不想列出数百个属性...

0 个答案:

没有答案