使用java在mongodb中有所区别

时间:2016-05-06 10:22:02

标签: java mongodb distinct

如何使用java代码区分mongodb中id的集合基础。

请建议我使用java代码获取不同的内容。

2 个答案:

答案 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"})