3

I've a cloudant documents that contains this specific structure a json array of category that contains array of subcategories , and i'm trying to write a cloudant query that returns documents with given category_id (for example 128) my problem that it's an array . for example :

"category": {
    "sub_category": [
      {
        "name": {
          "en": "Fast food"
        },
        "category_id": "127"
      },
      {
        "name": {
          "en": "Resturans"
        },
        "category_id": "128"
      }
    ],
}

so far i've tried to write this index :

{
   "index": {},
   "type": "text"
}

to index everything and a selector :

{"category.sub_category":{"$elemMatch" : {"category_id": {"$gt": 128" }}}

but it doesn't work. any help?

1 Answer 1

4

It looks like you are storing category_id as a string, but then trying to perform a numeric comparison. If you change your category_ids to integers:

"category_id": 127
...
"category_id": 128

then this selector should work:

"category.sub_category": {
    "$elemMatch" : {
        "category_id": {"$gt": 126 }
    }
}

Note: I changed the value in the query to 126 because your sample values were 127 and 128 and would not pass the original query (> 128).

Alternatively, if you wanted to keep category_id as a string and you wanted to find all documents where the category_id equals "128" then you could use the following selector:

"_id": { "$gt": null },
"category.sub_category": {
    "$elemMatch" : {
        "category_id": {"$eq": "128" }
    }
}
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.