Reputation: 97
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?
Upvotes: 3
Views: 3045
Reputation: 3491
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" }
}
}
Upvotes: 4