使用Java获取集合中所有键的名称
在此集合中(称为“播放”) 如何使用Java检索密钥的名称 示例输出:使用Java获取集合中所有键的名称,java,mongodb,Java,Mongodb,在此集合中(称为“播放”) 如何使用Java检索密钥的名称 示例输出: [ "_id" , "outlook" , "temp" , "humidity" , "windy" , "lable" ] 到目前为止,代码看起来是这样的 public class test { public static void main(String[] args) { DB dbtest = connection.dbconn(); DBCollection collec
[ "_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中查看,它将返回一个,这是方法的子类型。该字段是一个集合
,带有说明
此对象中字段的名称
因此,您可以使用
for (String key: cursor.next().keySet()) {
// do whatever with the key name here, f.ex.
System.out.println(key);
}
如果在API中查看,它将返回一个,这是方法的子类型。该字段是一个集合
,带有说明
此对象中字段的名称
因此,您可以使用
for (String key: cursor.next().keySet()) {
// do whatever with the key name here, f.ex.
System.out.println(key);
}
我们不会为你写代码。您已经有了一个良好的开端,为什么不尝试将
cursor.next()
赋值给一个变量,看看您可以对该变量做些什么。我只需要键名。不需要值我投票结束这个问题,因为它不是一个代码编写服务。@MarcoBonelli:也许他只需要一个指向API的指针。@MarcoBonelli答案有效,我得到了预期的答案,我们不会为您编写代码。您已经有了一个良好的开端,为什么不尝试将cursor.next()
赋值给一个变量,看看您可以对该变量做些什么。我只需要键名。不需要值我投票结束这个问题,因为它不是一个代码编写服务。@MarcoBonelli:也许他只需要一点指向API的指针。@MarcoBonelli答案有效我得到了预期的答案