如何计算集合中的唯一元素?

时间:2014-09-30 12:41:28

标签: java mongodb nosql

我有像

这样的集合
id : 1, url : youtube.com

也可以多次使用一个网址,

我需要获得整个收藏并计算独特的元素 像

youtube 10
google   8
lycos    5

这是代码

  public List<URLEntity> findAll() {
        List<URLEntity> list = new ArrayList<URLEntity>();
        String sort = "searchDate";
        String order = "desc";
        DBObject sortCriteria = new BasicDBObject(sort, "desc".equals(order) ? -1 : 1);

        BasicDBObject query = new BasicDBObject();           
        DBCursor cursor = mongoCoreService.getDomainCollection().find(query).sort(sortCriteria);
        try {
            while (cursor.hasNext()) {
                DBObject document = cursor.next();
                URLEntity entity = new URLEntity();
                entity = Converter.toObject(URLEntity.class, document);
                list.add(entity);
            }
        } finally {
            cursor.close();
        }
        return list;
    }

感谢

2 个答案:

答案 0 :(得分:3)

您可以使用MongoDB中的聚合框架来实现此目的。

db.yourcollection.aggregate({ $group: { _id: '$url', total: {$sum: 1} }})

你将获得这样的东西,你可以在JAVA中操作:

{
"result" : [ 
    {
        "_id" : "youtube.com",
        "total" : 10
    }, 
    {
        "_id" : "google.com",
        "total" : 8
    }, 
    {
        "_id" : "lycos",
        "total" : 5
    }
],
"ok" : 1
}

答案 1 :(得分:1)

今天我在java中找到并测试了答案,这是一个代码

DBCollection mycoll= db.getCollection("domain");

    DBObject fields = new BasicDBObject("domain", 1);
    DBObject project = new BasicDBObject("$project", fields );

    // Now the $group operation
    DBObject groupFields = new BasicDBObject( "_id", "$domain");
    groupFields.put("total", new BasicDBObject( "$sum", 1));
    DBObject group = new BasicDBObject("$group", groupFields);

    // run aggregation
    AggregationOutput output = mycoll.aggregate(project, group);
    System.out.println(output.getCommand().toString());

    for (DBObject dbObject : output.results()) {
        System.out.println(dbObject);
    }