2016-06-06 11 views
0

In dem folgenden Dokument wie bekomme ich das Feld filename in Java?Suchen Sie ein einzelnes inneres Feld in MongoDB mit Java

> db.Audit.findOne({"auditorComments.filepath":"NewAudit6269242818132644222/withoutJS.txt"},{'auditorComments.$':1}) 
{ 
     "_id" : ObjectId("573a02dbe4b017e93448707b"), 
     "auditorComments" : [ 
       { 
         "username" : "[email protected]", 
         "date" : ISODate("2016-05-20T23:59:29.889Z"), 
         "filename" : "withoutJS.txt", 
         "filepath" : "NewAudit6269242818132644222/withoutJS.txt", 
         "comment" : "test", 
         "bugnumber" : "1234" 
       } 
     ] 
} 

Ich habe einen Java-Code, aber es gibt immer null

public String getFilename(String key) { 
     DBCursor cur = audit.find(new BasicDBObject("auditorComments.filepath",key),new BasicDBObject ("auditorComments.$",1)); 
     String test = null; 
     while (cur.hasNext()) { 
      test = (String) cur.next().get("auitorComments.filename"); 
      System.out.print(test); 
     } 

     return null; 
    } 

Antwort

0

Versuchen Sie folgendes:

public String getFilename(String key) { 
    DBCursor cur = audit.find(new BasicDBObject().append("auditorComments.filepath", "NewAudit6269242818132644222/withoutJS.txt")); 
    String test = null; 
    while (cur.hasNext()) { 
     BasicDBObject current_object = (BasicDBObject) cur.next(); 

     BasicDBList auditorComments = (BasicDBList) current_object.get("auditorComments"); 
     test = ((BasicDBObject) auditorComments.get(0)).getString("filename"); 
     System.out.println(test); 
    } 
    return test; 
} 
Verwandte Themen