下面是我正在使用的代码-
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
最佳答案
使用 mongoshell 插入时,如果您想要整数值,则需要明确指定 NumberInt(0) 或 NumberLong(0)。否则,MongoDB 将使用浮点值。
当你在 mongodb shell 中执行 find() 时,如果它是整数,它也会返回 NumberInt(0) 或 NumberLong(0) 。
关于java - 如何使用 MongoDB Java 驱动程序返回正确的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32625607/