Query Nested JSON.

0 votes

Consider a collection with JSON

{
  "username": "9000000000",
  "data": {
    "id": 0,
    "lastName": "kumar",
    "sex": 1,
    "email": "ram@kumar.co.in",
    "firstName": "ram",
    "mobile": "9000000000",
    "city": "Mumbai"
  },
  "type": 1
}
 
I want to get  such documents with
type==1 AND (mobile LIKE xyz OR email==xyz@gmail.com)
 
Query qEmail=QueryBuilder.build(ModelPhone.KEY_EMAIL, model.getEmail(), Operator.EQUALS);
Query qMobile=QueryBuilder.build(ModelPhone.KEY_MOBILE, "%"+model.getMobile(), Operator.LIKE);
Query qOR=QueryBuilder.compoundOperator(qEmail, Operator.OR, qMobile);
 
Query qFinal=QueryBuilder.compoundOperator(qType, Operator.AND, qOR);
Storage storage=getStorageService().findDocumentsByQuery(dbName, COLLECTION_USERDATA, qFinal);
 
Out Put:
getUserData() : com.shephertz.app42.paas.sdk.java.App42NotFoundException: {"app42Fault":{"httpErrorCode":404,"appErrorCode":2608,"message":"Not Found","details":"No document in the collection 'collection_userdata' exists for given query"}}
 
 
So how do I query Nested Data???
 
asked Jun 17, 2015 in App42 Cloud API-BaaS by android.dreamscapeme (11 points)

1 Answer

0 votes

Hello,

Could you please share the internal logs of this method with us. It will help us to see the exact query which you are applying in this method to find the documents. To print the internal logs, put the below line of code before making a findDocumentsByQuerymethod call: 

App42Log.setDebug(true);

Also please share qType query which you set in compound statement of qFinal. 

Thanks,

Himanshu Sharma

answered Jun 17, 2015 by hs00105 (2,005 points)
Query qType=QueryBuilder.build(ModelUserData.KEY_TYPE, ModelUserData.TYPE_MODEL_PHONE,  Operator.EQUALS);
well I figured out how to query nested json.. instead of just "mobile" it was needed "data.mobile". Thank you.

One more thing:
Query qMobile=QueryBuilder.build(ModelPhone.KEY_MOBILE, "%"+model.getMobile(), Operator.LIKE);
is it a valid query?
using % and _ as in SQL like query.
Hello,

Great to hear that you figure out your issue. About to use "%"  operator, you don't need to use this operator for like search. Just pass your query as shown below:

Query qMobile=QueryBuilder.build(ModelPhone.KEY_MOBILE, model.getMobile(), Operator.LIKE);

Regards,
Himanshu Sharma
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...