【问题标题】:How to search through multiple documents having multiple fields for a keyword using index "text" in MongoDB?如何在 MongoDB 中使用索引 \"text\" 搜索具有多个关键字字段的多个文档?
【发布时间】: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


    【解决方案1】:

    $或是你正确的做法:https://www.mongodb.com/docs/manual/reference/operator/query/or/

    只需将它与。寻找如示例链接所示,您将获得一系列文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2011-09-14
      • 2022-01-26
      • 2013-03-21
      相关资源
      最近更新 更多