查询特定的Mongodb字段

时间:2012-07-27 15:47:33

标签: java mongodb

我需要将以下查询转换为Java代码

使用dbName
db.collectionName.find({},{参考:1})

我正在尝试获取键'reference'的值

我尝试了以下但是这似乎不是那种方式

  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();
        }

1 个答案:

答案 0 :(得分:0)

尝试以下

basicDBObject uqery = new BasicDBObject();
query.put("reference", 1);
DBCursor cursor = profilecollection.find(query);

您不需要指定{} ...它是返回所有字段的默认选项