我有一个包含嵌套对象数组的文档,其中包含一长串属性。我不需要搜索文档,而是需要在一个文档的嵌套数据中进行搜索。
(嵌套属性的索引类似于 '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"
...
}
]
}