Find a single inner field in MongoDB using java -


in below document how filename field in java ?

> db.audit.findone({"auditorcomments.filepath":"newaudit6269242818132644222/withoutjs.txt"},{'auditorcomments.$':1}) {         "_id" : objectid("573a02dbe4b017e93448707b"),         "auditorcomments" : [                 {                         "username" : "c9-security-1_ww@oracle.com",                         "date" : isodate("2016-05-20t23:59:29.889z"),                         "filename" : "withoutjs.txt",                         "filepath" : "newaudit6269242818132644222/withoutjs.txt",                         "comment" : "test",                         "bugnumber" : "1234"                 }         ] } 

i have java code returns 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;     } 

try this:

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