2013-12-08 63 views
0

MongoDB的查詢與「平等」我有以下BSON MongoDB中如何用Java

{ 
    "_id" : ObjectId("52a30bdfaaa83ba83c25c0f3"), 
    "course" : { 
     "course_name" : "Java EE", 
     "description" : "Java Enterprise Edition" 
    }, 
    "first_name" : "Lucy", 
    "last_name" : "Hill", 
    "gender" : "female" 
} 

我想找到所有具有「COURSE_NAME」 =「的Java EE」的文件。在Java中,我試過這段代碼,但它似乎沒有工作。

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()); 
} 

任何人都可以幫助我嗎?

回答

2
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()); 
} 

您可以使用.訪問子文檔中的字段。

Official documentation on querying