Januka samaranyake
Januka samaranyake

Reputation: 2597

FieldPath field names may not contain '.'

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 ?

Upvotes: 3

Views: 4528

Answers (2)

Aanchal Mehrotra
Aanchal Mehrotra

Reputation: 99

The keys used in the _id field of $group will be the keys in the resulting documents. Mongodb does not allow periods in the field names as they are used to access nested fields.

The correct query should be :

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"
        }
    }
}

])

Upvotes: 0

Dinal24
Dinal24

Reputation: 3192

Issue is access.times, It is a bad practice to have . in your fields as it collides with nested elements.

Upvotes: 2

Related Questions