-1
([{
    $match: {
        "Publication_Date": {
            $gte: ISODate("2003-01-01T00:00:00.0Z")
        },
    }
   }, {
    $group: {
        _id: {
            $year: "$Publication_Date"
        },
        total: {
            $sum: 1
        },
    }
}])
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • 1
    Please consider including sample document from your collection and the code you have tried so far – s7vr Mar 23 '17 at 18:20

1 Answers1

0

Just "translate" this into org.bson.Document structure (similar to map) and call proper action (it is not a query, in that case it is an aggregate):

Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2003-01-01");
Document group = new Document();
group.put("_id", new Document("$year", "$Publication_Date"));
group.put("total", new Document("$sum", 1));
AggregateIterable<Document> aggregate = collection.aggregate(Arrays.asList(
    new Document("$match", new Document("Publication_Date",
        new Document("$gte", date))),
    new Document("$group", group)
));

or use com.mongodb.client.model package static methods (less verbose):

AggregateIterable<Document> aggregate = collection.aggregate(Arrays.asList(
    Aggregates.match(Filters.gte("Publication_Date", date)),
    Aggregates.group(
        new Document("$year", "$Publication_Date"),
        Accumulators.sum("total", 1))));
  • if i have one another field which name is publicationId and i have one set of the publicationid and group by them the how can i do that? because this query apply for all the document and i want to apply some fix document which publicationid is in the set – RAHUL KUKADIYA Mar 25 '17 at 05:20