Java mongodb 3.0 driver query without filter
•
Java
How do I query using the Java mongodb 3.0 driver?
I tried to query unique category records from the location collection in mongodb In the Mongo shell, this is simple: dB locations. distinct(“categories”);
In Java, it is different
MongoClient client = new MongoClient(); MongoDatabase db = client.getDatabase("yelp"); //this will not compile,although examples from before 3.0 do it this way MongoCursor<Document> c = db.getCollection("locations").distinct("categories").iterator();@H_502_16@
Solution
To avoid casting with distinct, the mongocollection API allows you to provide different expected value types for fields Therefore, if you know that they are all strings, you can write:
MongoCursor<String> c = db.getCollection("locations").distinct("categories",String.class).iterator();@H_502_16@或所有数字:
MongoCursor<Number> c = db.getCollection("locations").distinct("categories",Number.class).iterator();@H_502_16@你仍然可以这样做:
MongoCursor<Object> c = db.getCollection("locations").distinct("categories",Object.class).iterator();@H_502_16@如果你不能保证你正在查询的字段值的类型.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码