使用 Java 获取集合中所有键名称的名称 [英] Get names of all keys name in collection using Java
本文介绍了使用 Java 获取集合中所有键名称的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这个集合中(称为播放")
In this collection (called "play")
{ "_id" : 0 , "outlook" : "sunny" , "temp" : "hot" , "humidity" : "high" , "windy" : "weak" , "lable" : "no"}
{ "_id" : 1 , "outlook" : "sunny" , "temp" : "hot" , "humidity" : "high" , "windy" : "strong" , "lable" : "no"}
{ "_id" : 2 , "outlook" : "overcast" , "temp" : "hot" , "humidity" : "high" , "windy" : "weak" , "lable" : "yes"}
{ "_id" : 3 , "outlook" : "rain" , "temp" : "mild" , "humidity" : "high" , "windy" : "weak" , "lable" : "yes"}
{ "_id" : 4 , "outlook" : "rain" , "temp" : "cool" , "humidity" : "normal" , "windy" : "weak" , "lable" : "yes"}
{ "_id" : 5 , "outlook" : "rain" , "temp" : "cool" , "humidity" : "normal" , "windy" : "strong" , "lable" : "no"}
如何使用 Java 检索密钥的名称?
How can I retrieve the names of the keys using Java?
示例输出:
[ "_id" , "outlook" , "temp" , "humidity" , "windy" , "lable" ]
目前的代码是这样的
public class test {
public static void main(String[] args) {
DB dbtest = connection.dbconn();
DBCollection collection = dbtest.getCollection("play");
BasicDBObject allQuery = new BasicDBObject();;
DBCursor cursor = collection.find(allQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/**
write code.. here...
*/
}
}
推荐答案
如果你在 API 中查看 DBCursor.next()
,它返回一个DBObject
,它是 BSONObject
带有 keySet()
方法.该字段是一个Set
,带有描述
If you look in the API at DBCursor.next()
, it returns a DBObject
, which is a subtype of BSONObject
with a keySet()
method. That field is a Set<String>
, with the description
该对象中字段的名称
因此,您可能可以使用
for (String key: cursor.next().keySet()) {
// do whatever with the key name here, f.ex.
System.out.println(key);
}
这篇关于使用 Java 获取集合中所有键名称的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文