0

I want to query an array of objects with a particular key should have text in it.

this is the query I have tried

to find disclaimer.text exists and not empty. But It always prints 0

 db.slideshows.count({"config.slides": { $elemMatch: {disclaimer: {"text" :   {"$exists" : true, "$ne" : ""}  }} } })

This is my data

 {
 "id": 1002,
 "config": {
"firstSlide": "vehicle",
"slides": [
  {
    "slideKey": "sk1",
    "disclaimer": {
      "text": ""
    }
  },
  {
    "slideKey": "sk2"
   }
  ]
 }
}

{
"id": 1003,
"config": {
"firstSlide": "book",
"slides": [
  {
    "slideKey": "sk3",
    "disclaimer": {
      "text": "Hello"
    }
  },
  {
    "slideKey": "sk4"
   }
  ]
 }
}

{
"id": 1004,
"config": {
"firstSlide": "book",
"slides": [
  {
    "slideKey": "sk3",
    "disclaimer": {
      "text": "nope"
    }
  },
  {
    "slideKey": "sk4",
    "disclaimer": {
      "text": ""
    }
   }
  ]
  }
 }

I want all the rows which have not empty disclaimer.text. ex. in the above set I need to get id 1004 and 1003 as a result.

1 Answer 1

1

Try

db.slideshows.count({
  "config.slides": {
    $elemMatch: {
      "disclaimer.text": {
        "$exists": true,
        "$ne": ""
      }
    }
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.