MongoDb Java通过自定义_id

时间:2016-06-21 15:57:23

标签: java mongodb

我在通过id获取单个文档时遇到了一些麻烦(我插入了我自己的id' s。)

我看过驱动程序文档,但我找不到任何能告诉我如何获取单个文档的内容。

我的收藏如此

{ "_id" : NumberLong(1), "currency_pair" : "GBP/USD", "date" : "2009-05-01 00:00:00", "sell" : "1.47993", "buy" : "1.48076", "spread" : 0.0008300000000001084 }
{ "_id" : NumberLong(2), "currency_pair" : "GBP/USD", "date" : "2009-05-01 00:00:00", "sell" : "1.48023", "buy" : "1.48076", "spread" : 0.0005300000000001415 }
{ "_id" : NumberLong(3), "currency_pair" : "GBP/USD", "date" : "2009-05-01 00:00:01", "sell" : "1.48023", "buy" : "1.48077", "spread" : 0.0005399999999999849 }

我当前的查询是这样的。

        if (!Main.REMOTE) {
            server = "localhost";
        }

        MongoClient mongoClient = new MongoClient( server );
        MongoDatabase db = mongoClient.getDatabase("rates");
        MongoCollection<Document> coll = db.getCollection(tableName);

        BasicDBObject query=new BasicDBObject("_id", 2);
        coll.find(query);

1 个答案:

答案 0 :(得分:0)

尝试:

BasicDBObject query=new BasicDBObject("_id", NumberLong(2));