我想querymongodb收集得到一个特定的ID字段 - (I am trying to quer

2019-11-04 15:57发布

我想从最近更新(“timeSliceStart”)行_id已符合“EmployeeName”

DBCursor cursor = collection.find(EmployeeName).sort(new BasicDBObject("timeSliceStart", -1)).limit(1);
 while(cursor.hasNext()) {
            String result = cursor.next().get("id"); }

在蒙戈DB数据::

{ "_id" : { "employeeId" : "1234567890", "@objectName" : "FeedMetadata" }, "school" : false, "college" : null, "errorCount" : 0, "errors" : [], "EmployeeName" : "Peter" }

这是获得打印结果为: { "employeeId" : "1234567890" , "@objectName" : "FeedMetadata"}

我只想从_id的雇员,我试过_id.employeeId ; ,但它返回null 。 有没有只显示雇员的方法吗?

我的Java程序 -

  BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("EmployeeName", empName);
    DBCursor cursor = collection.find(whereQuery).sort(new BasicDBObject("lastUpdate", -1)).limit(1);
    while(cursor.hasNext()) {
        String result = cursor.next().get("_id").toString();
        System.out.println(result);
    }

Answer 1:

你可以改变这样while循环:

while(cursor.hasNext()) {
    String result = ((HashMap)((BasicDBObject)(cursor.next().get("_id")))).get("employeeId");
    System.out.println(result);
}


文章来源: I am trying to querymongodb collection to get one particular id field -