【问题标题】:$text $search on array field in mongoDB with Nodejs使用 Nodejs 在 mongoDB 中的数组字段上搜索 $text $search
【发布时间】:2019-05-30 12:45:45
【问题描述】:

我想在 mongoDB 集合上执行 $text $search,其中文档的字段是字段数组。

由于某种原因,我的查询返回 false。无法弄清楚,因为我已经在集合中的“conditions.description”上放置了索引。

集合中的示例文档

{
  "_id": "58f9c87b5246af0aac145ew",
  "total": 2,
  "patientId": "xxxxxxx",
  "conditions": [
    {
      "verificationStatus": "confirmed",
      "dateRecorded": "2017-03-14",
      "clinicalStatus": "active",
      "description": "Afib"
    },
    {
      "verificationStatus": "confirmed",
      "dateRecorded": "2017-03-14",
      "clinicalStatus": "active",
      "description": "Arterial hypertension"
    }
  ]
}

我的 MongoDB 查询返回 false


.find({
       "$and": [
          { "patientId": { $eq: pID } },
          { "conditions.description": { $text: { $search: "diabetes hypertension" }, $caseSensitive: false } }
        ]
})

【问题讨论】:

    标签: arrays node.js mongodb


    【解决方案1】:

    根据documentation,您没有为$text 搜索提供字段:

    db.getCollection('YourCollection').find({
       "$and": [
          { "patientId": { $eq: pID } },
          { $text: { $search: "diabetes hypertension" } }
        ]
    })
    

    【讨论】:

    • 是的,我认为这就是我的问题所在。我不相信我可以用 $text 完成我所需要的。我将不得不以旧方式执行此操作,因为该字段是具有子嵌套数组的数组字段。如果我为特定的患者 ID 拉取文档,我可以遍历数据并为搜索词执行 indexOf 并基于此返回 true 或 false。
    • 好的。我不确定我是否应该为 Akrion 放弃一个。问题是我根本无法使用 $text $search 完成我想要的事情。但他说的没错。有什么想法的人???你认为我应该为他的回答做些什么?
    猜你喜欢
    • 2013-11-19
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多