[英]Querying for specific Mongodb field
I need to have the following query converted in to Java code 我需要将以下查询转换为Java代码
use dbName 使用dbName
db.collectionName.find({},{reference:1}) db.collectionName.find({},{参考:1})
I am trying to fetch the value for the key 'reference' 我正在尝试获取键“参考”的值
I tried the following but this doesn't seem to be the way 我尝试了以下方法,但这似乎不是这样
Mongo dbConnection;
DB dbobject;
dbConnection = new Mongo("localhost", 27017);
dbobject = dbConnection.getDB("dbName");
DBCollection profileCollection = dbobject.getCollection("collectionName");
BasicDBObject query = new BasicDBObject();
query.put("{},{reference:1}");
DBCursor mongocursor =profileCollection.find("{}, {reference:1}");
try {
while(mongocursor.hasNext()) {
System.out.println(mongocursor.next().get("reference"));
}
} finally {
mongocursor.close();
}
Try the following 尝试以下
basicDBObject uqery = new BasicDBObject();
query.put("reference", 1);
DBCursor cursor = profilecollection.find(query);
you do not need to specify {} ... it is a default options for returning all the fields 您无需指定{} ...这是用于返回所有字段的默认选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.