我正在尝试从上次更新(" timeSliceStart")行获取_id,该行具有匹配" EmployeeName"
DBCursor cursor = collection.find(EmployeeName).sort(new BasicDBObject("timeSliceStart", -1)).limit(1);
while(cursor.hasNext()) {
String result = cursor.next().get("id"); }
Mongo DB中的数据::
{
"_id" : {
"employeeId" : "1234567890",
"@objectName" : "FeedMetadata"
},
"school" : false,
"college" : null,
"errorCount" : 0,
"errors" : [],
"EmployeeName" : "Peter"
}
正在打印的结果是:
{ "employeeId" : "1234567890" , "@objectName" : "FeedMetadata"}
我想要来自_id的employeeId,我尝试了_id.employeeId ;
,但它返回了null
。
有没有办法只显示employeeId?
我的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);
}
答案 0 :(得分:0)
您可以像这样更改while循环:
while(cursor.hasNext()) {
String result = ((HashMap)((BasicDBObject)(cursor.next().get("_id")))).get("employeeId");
System.out.println(result);
}