我想通过Java中的mongoDB中匹配的名称获取ID。
这是我的代码:
MongoClient mongo = new MongoClient();
DB db = mongo.getDB("test");
DBCollection groupTable = db.getCollection("Items");
searchQuery.put("name", "John");
DBCursor cursor = groupTable.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
但它显示整行包含名字 John,我想要的是 ID 而不是整个内容。
{"list":[{"timestamp":{"$date":"2014-08-01T08:37:54.058Z"},"name":John,"_id":{"$oid":"53db5045ccf2b2399e0e6128"},"created":{"$date":"2014-08-}
有什么帮助吗? 谢谢
最佳答案
很简单,选择id即可...
while (cursor.hasNext()) {
System.out.println(cursor.next().get("_id"));
}
关于java - Java如何通过匹配mongoDB中的名称来获取元素的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134749/