如何使用java代码区分mongodb中id的集合基础。
请建议我使用java代码获取不同的内容。
答案 0 :(得分:2)
首先获得连接
mongoClient = new MongoClient("localhost", 27017);
chatsDB = mongoClient.getDatabase("productfeeds");
docCollection = chatsDB.getCollection("products");
然后使用此查询;
ArrayList<String> prodRetailer = docCollection.distinct("merchant_name", String.class)
.filter(new Document("merchant_name"))
.into(new ArrayList<String>());
你得到了不同的清单
答案 1 :(得分:0)
使用聚合管道:
db.foo.aggregate({_id: "$myDistinctField"})