0

下面是我正在使用的代码 -

mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("sample");
MongoCollection<Document> imagescollaction = database.getCollection("images");
System.out.println("_id value  : " + imagescollaction.find().first().get("_id"));

程序输出:_id 值:0.0

从 mongo shell 的图像集合中输出

db.images.find();

{ "_id" : 0, "height" : 480, "width" : 640, "tags" : [ "dogs", "work"
] }     
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : [ "cats",
"sunrises", "kittens", "travel", "vacation", "work" ] }

我的问题:从 mongo shell 我可以看到这些值是 Long 或 Integer,但是当我使用 find 方法从 java mongo 驱动程序查询时,它正在打印 Double。有谁知道为什么会这样。

我正在使用的 Mongo 驱动程序:3.0.0

4

1 回答 1

3

使用 mongoshell 插入时,如果您想要整数值,则需要明确地说 NumberInt(0) 或 NumberLong(0)。否则,MongoDB 将使用浮点值。

当您在 mongodb shell 中执行 find() 时,如果它是整数,它还将返回 NumberInt(0) 或 NumberLong(0)。

于 2015-09-17T08:49:58.643 回答