0

I want to do a $text $search on a mongoDB collection where the documents have a field that is an array of fields.

My query is returning false for some reason. Can't figure it out as I've already put an index on "conditions.description" in the collection.

Example Document in collection

{
  "_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"
    }
  ]
}

My MongoDB Query that is returning false


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

1 Answer 1

1

As per the documentation you do not provide field for the $text search:

db.getCollection('YourCollection').find({
   "$and": [
      { "patientId": { $eq: pID } },
      { $text: { $search: "diabetes hypertension" } }
    ]
})
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I think that's what my problem is. I don't believe I can accomplish what I need with $text. I'll have to do it the old way because the field is an array field with sub-nested arrays. If I do a pull of the document for specific patientId, I can loop through the data and do an indexOf for the search terms and return true or false based upon that.
Ok. I'm not sure if I should give the one up for Akrion. The problem was that I couldn't accomplish what I wanted using $text $search...at all. But he was correct in what he said. Any thoughts people??? What do you think I should do for his answer?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.