Welcome
Login
Sign up
Home
Solutions
Forums
How can we help you today?
Enter your search term here...
Search
Login
or
Signup
to submit a new ticket
Check ticket status
Start a new topic
Discussions
Kinvey Forums
Android
How to query using $elemMatch?
R
Ruben
started a topic
over 6 years ago
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:
db.products.find({"items.shoppinglists":{$elemMatch:{
"shoppinglistid":"52602d3582d850d77f001466",
"state":"pending"
}}})
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:
mDataStore.query.in("items.shoppinglists.shoppinglistid", arrayshoppinglistids)
.equals("items.shoppinglists.state","pending");
Is the command elemMatch not supported? Is there any other workaround?
Thanks in advance,
Ruben.
2 Comments
Oldest First
Popular
Newest First
Sorted by
Oldest First
E
Edward
said
over 6 years ago
Hey,
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);
R
Ruben
said
over 6 years ago
Great! Thanks for your help :)
Login
or
Signup
to post a comment
More topics in
Android
Can I add KinveyReferences and other custom Arrays to the User entity?
How can I use custom enums within my Entity?
Linking an image does not save with LinkedData
How can I get all records saved by the current user?
Why don't all users see the same data when querying?
Retrieving related file from datastore collection - continued
Problem implementing sign up / login
Can Android API be used for Java?
I'm not receiving push notifications
It is impossible to receive object according to the link
See all 259 topics
Ruben
db.products.find({"items.shoppinglists":{$elemMatch:{
"shoppinglistid":"52602d3582d850d77f001466",
"state":"pending"
}}})
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:
mDataStore.query.in("items.shoppinglists.shoppinglistid", arrayshoppinglistids)
.equals("items.shoppinglists.state","pending");
Is the command elemMatch not supported? Is there any other workaround?
Thanks in advance,
Ruben.