I'm having a little trouble with a query in Java on a MongoDB.
I have the follow structure in the database:
{
"_id" : ObjectId("5059c214707747cbc5819f6f"),
"id" : "7",
"title" : "test4",
"keywords" : "keyword 1, keyword 2",
"partner" : {
"id" : "1",
"name" : "partner",
"keywords" : "partner keyword 1, partner keyword 2"
},
"status" : {
"id" : "0",
"name" : "Expired"
},
"modified" : "2012-09-27"
}
I want the query the database for the field 'Status.name', example SELECT * FROM table WHERE status.name = 'Expired'
How would I do such a query in Java for MongoDB?
Thanks for any help or suggestions!
With MongoDB Java Driver v3.2.2 you can do something like this:
This returns all documents containing "Expired" in the
status.name
nested field.MongoDB uses a 'match object' as a query. So to find objects that have
status.name
equal to "Expired", you could feed an object like such:{ "status.name": "Expired" }
From Java you'll need to create a
DBOjbect
like the above to use as the match (returning everything that ... matches with the match object) and send that to the database as a query. Assuming you'll have a connection to MongoDB from Java, use the find-method to execute the query you're after.This will print all the rows having status as "Expired"
Here is an example: