Hi,
I’m trying to add a project stage to a pipeline in Java code.
My collection contains the fields:
_id, instrument, ctype, cname, pname, alias, timestamp, value
The purpose of my query is to get distinct groups of (ctype+cname+pname+alias) for a given instrument and a range of dates.
My code -before adding project stage- is:
AggregateIterable<Document> parameters = valuesCollection.aggregate(
Arrays.asList(
Aggregates.match(Filters.and(Arrays.asList(
Filters.eq(instrument, 35),
Filters.gte(timestamp, fromDate),
Filters.lte(timestamp, toDate)
))),
Aggregates.group(Filters.and(
Filters.eq(ctype, "$ctype"),
Filters.eq(cname, "$cname"),
Filters.eq(pname, "$pname"),
Filters.eq(alias, "$alias")))
));
The reason why I need to use a project stage is that the only field in the returned document is _id.
Adding the project stage, my query is now like this:
AggregateIterable<Document> parameters = valuesCollection.aggregate(
Arrays.asList(
Aggregates.match(Filters.and(Arrays.asList(
Filters.eq(instrument, 35),
Filters.gte(timestamp, fromDate),
Filters.lte(timestamp, toDate)
))),
Aggregates.group(Filters.and(
Filters.eq(ctype, "$ctype"),
Filters.eq(cname, "$cname"),
Filters.eq(pname, "$pname"),
Filters.eq(alias, "$alias"))),
Aggregates.project(Projections.fields(Projections.excludeId(), Projections.include("ctype","cname","pname","alias")))
));
But it doesn’t work and returns no document.
Thanks for your help!