【发布时间】:2022-11-28 16:59:53
【问题描述】:
我想搜索文档中的任何字段中是否存在关键字。
所以我遇到了以下解决方案。mongo查询db.adds.aggregate([{$match:{$text:{$search:"hello"}}},{$lookup:{from:"companies",localField:"companyId",foreignField:"_id",as:"company"}},{$unwind:"$company"}])
结果`
[
{
_id: ObjectId("63832de4e9dbcd9b2942ded7"),
companyId: [ ObjectId("6383048ae9dbcd9b2942dece") ],
title: 'hello',
imageUrl: 'https://drive.google.com/file/image',
company: {
_id: ObjectId("6383048ae9dbcd9b2942dece"),
name: 'name1',
url: 'name1.com'
}
}
]
`
但问题是我有另一个文档,其中“title”的值为“hello”。通过使用上面的查询,它不会返回第二个文档。我该如何解决这个问题?
【问题讨论】:
标签: node.js mongodb mongodb-query full-text-search