简体   繁体   中英

Group by Field MongoDB Java Driver 3+

I would like to apply a group operation on my entire Document using MongoDB Java Driver 3.0

My query is something like:

db.coll.group( { key: {"field": 1}, cond: {}, reduce: function(curr, result){}, initial: {} } )

Results are:

{
        "field" : "A61038968K16X275KNWCEIHr"
    },
    {
        "field" : "AH1038968716P3210C6NiQpD"
    },
    {
        "field" : "AV1038968F16Q321DCxY7T6w"
    },
    {
        "field" : "A71038968K165321PLiEhbGJ"
    },
    {
        "field" : "AY1038968N16w321a537co1U"
    },
    {
        "field" : "AJ1038968E16S3212MJpeNNV"
    }

I'm trying things in Java like : collection.aggregate(group("field")) but it doesn't work. Sorry if it's easy but I can't find anything googling.

Thanks!

So, what I was looking for was a 'distinct' method.

My solution was:

collection.distinct(<string field>, <query>, <result class>);

For example, in my case, my result class is a String.

collection.distinct("field_grouping",gte("field_query", 1000),String.class);

After that you can iterate your results and do whatever you want:

Iterator<String> iterator = collection.distinct("field_grouping",gte("field_query", 1000),String.class).iterator();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM