Reputation: 233
I have following BSON in Mongodb
{
"_id" : ObjectId("52a30bdfaaa83ba83c25c0f3"),
"course" : {
"course_name" : "Java EE",
"description" : "Java Enterprise Edition"
},
"first_name" : "Lucy",
"last_name" : "Hill",
"gender" : "female"
}
I want to find all the document which has "course_name" = "Java EE". In java, I had tried this code but it didn't seem to be working.
DBCollection table = db.getCollection("Students"); //my Collection
BasicDBObject courseNameDBObject = new BasicDBObject()
.append("course", new BasicDBObject("course_name", "Java EE"));
DBCursor cursor = table.find(courseNameDBObject);
while(cursor.hasNext()){
System.out.println(cursor.next());
}
Could anyone help me?
Upvotes: 1
Views: 1303
Reputation: 5095
DBCollection table = db.getCollection("Students"); //my Collection
BasicDBObject courseNameDBObject = new BasicDBObject("course.course_name", "Java EE") ;
DBCursor cursor = table.find(courseNameDBObject);
while(cursor.hasNext()){
System.out.println(cursor.next());
}
You can use .
to access fields within a sub document.
Official documentation on querying
Upvotes: 3