For mongodb 3.2 following query successfully worked.
db.user_log.aggregate([{ "$match" : { "user_id" : "1" , "page_id" : "678761252281073"}} , { "$sort" : { "meta_data.access_times" : -1}} , { "$group" : { "_id" : { "first_name" : "$meta_data.user_data.first_name" , "last_name" : "$meta_data.user_data.last_name" , "profile_pic" : "$meta_data.user_data.profile_pic" , "user_id" : "$user_id" , "star_value" : "$star_value" , "access.times" : "$meta_data.access_times"}}}])
Java code -
Aggregation aggregation = newAggregation(
match(Criteria.where("user_id").is("123")),
sort(Sort.Direction.DESC, "meta_data.access_times"),
group(Fields.fields().and("first_name", "$meta_data.user_data.first_name").and("last_name", "$meta_data.user_data.last_name").and("profile_pic", "$meta_data.user_data.profile_pic").and("user_id", "$user_id").and("star_value", "$star_value").and("access.times", "$meta_data.access_times"))
);
AggregationResults<UsersMongoResult> groupResults = mongoTemplate.aggregate(aggregation, "user_log", UsersMongoResult.class);
but after upgrading to 3.4 it occurs following exception
org.springframework.dao.InvalidDataAccessApiUsageException: Command execution failed: Error [FieldPath field names may not contain '.'.]
what is the reason behind this and how do I fix the issue ?
Issue is access.times
, It is a bad practice to have .
in your fields as it collides with nested elements.