I have mongodb documents of following kind:
{
"_id" : {
"refId" : ObjectId("55e44dd70a975a6fec7ae66e"),
"someString" : "foo"
},
"someValue" : 1,
"date" : NumberLong("1441025536869"),
"subdoc" : {
"someRef" : ObjectId("xf2h55e44dd70a975a6fec7a"),
"count" : 99
}
}
Want to get list of documents with unique "subdoc.someRef"
with earliest "date"
(if there are any documents with same "subdoc.someRef"
) and sorted by any field. The issue is in sorting by field "subdoc.count"
.
I use following spring data aggregation:
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(match),
Aggregation.project("date", "someValue")
.and("subdoc.someRef").as("someRef")
.and("subdoc.count").as("count")
.and("_id.someString").as("someString")
.and("_id.refId").as("refId"),
Aggregation.sort(Sort.Direction.ASC, "date"),
Aggregation.group("someRef")
.min("date").as("date")
.first("refId").as("refId")
.first("someValue").as("someValue")
.first("count").as("count"),
Aggregation.project("someValue", "date", "refId", "count")
.and("_id").as("someRef"),
Aggregation.sort(pageable.getSort()),
Aggregation.skip(pageable.getOffset()),
Aggregation.limit(pageable.getPageSize())
);
Everething is fine except how spring data converts: .first("count").as("count")
I always got "count"
: null in aggregation result.
DBObject created by spring is not what I've expected. That line in log concerns me:
"$group" : { "_id" : "$someRef" , "date" : { "$min" : "$date"} , "refId" : { "$first" : "$_id.refId"} , "someValue" : { "$first" : "$someValue"} , "count" : { "$first" : "$subdoc.count"}}}
I cannot understand why it always puts "$subdoc.count"
instead of putting "$count"
as result of previous step of aggreagation pipeline. So "count" in result is always null as "$subdoc.count"
is always null in last grop step.
How to make Spring use the value that I want instead of putting reference?