$ text $使用Node.js在mongoDB中的数组字段上搜索

时间:2019-05-30 04:29:31

标签: arrays node.js mongodb

我想对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查询返回假


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

1 个答案:

答案 0 :(得分:0)

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

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