0

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

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

我正在尝试获取键“参考”的值

我尝试了以下方法,但这似乎不是这样

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

1 回答 1

0

尝试以下

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

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

于 2012-07-27T18:09:32.560 回答