Start a new topic

How to query using $elemMatch?

Hello, I'm trying to retrieve items matching multiple conditions in a subarray but can't find the query method in Android, in mongoDB works fine:





Sample of the data:


"_id" : "541485d974c3eae62603dc1b",

"items" : [


"content" : 1,

"price" : 1,

"quantity" : 1,

"shoppinglists" : [


"quantity" : 1,

"shoppinglistid" : "52602d3582d850d77f001466",

"state" : "pending"





"product" : "Milk",

"_acl" : {

"creator" : "52601ac17c932fc07e00187b"


"_kmd" : {

"lmt" : "2014-09-13T17:58:53.267Z",

"ect" : "2014-09-13T17:58:49.223Z"



The best aproximation I've find is this but doesn't return the desired data:"items.shoppinglists.shoppinglistid", arrayshoppinglistids)


Is the command elemMatch not supported? Is there any other workaround?

Thanks in advance,



so this can be accomplished with a workaround, by overloading the `equals` method. Use this in combination with the fact that a `GenericJson` is just a HashMap, and you can access any mongo operators that might not be wrapped by the library.

GenericJson elems = new GenericJson();

elems.put("shoppinglistid", "52602d3582d850d77f001466");

elems.put("state", "pending");

GenericJson shopping = new GenericJson();

shopping.put("$elemMatch", elems);

Query q = new Query().equals("items.shoppinglists", shopping);

Great! Thanks for your help :)
Login or Signup to post a comment