0

I am trying to run aggregation query where trying to unwind the array which is nested under the key customFields.

Structure is like this

{
    "note" : {
        "customFields" : {
            "externalAttendees" : [ 
                {
                    "email" : "someemail@domain.com",
                    "epp" : null
                }, 
                {
                    "email" : "anotheremail@domain.com",
                    "epp" : null
                }
            ]
        }
    }
}

customField is Map of type string, Object. When I am trying to unwind note.customFields.externalAttendees then getting error No property externalAttendees found for type Object! Traversed path: Activity.note.customFields.

On the other side if I unwind on the field which is an array of concrete object like List where User class have email and epp as instance variable then I am not getting any exception.

Is there any way to unwind on dynamic keys which are not part of bean but available in document.

I am using springMongoData db aggregation.

Sandeep
  • 233
  • 4
  • 13

1 Answers1

0

Looks like document structure should have exact same java representation. As in my case externalAttendees on which I was grouping is part of Map not instance variable of bean.

I tried executeCommand(BasicDBObject.parse(stringJson)) of MongoTemplate and it solve the purpose.

my stringJson will look like

{ "aggregate" : "__collection__" , "pipeline" : [ { "$match" : { "note.customFields.externalAttendees" : { "$exists" : true}}} , { "$match" : { "note.customFields.externalAttendees.name" : { "$in" : [ "x" , "y"]}}} , { "$sort" : { "createdDate" : -1}} , { "$unwind" : "$note.customFields.externalAttendees"} , { "$project" : { "id" : 1 , "emailId" : "$note.customFields.externalAttendees.name" , "aType" : "$note.type" , "subject" : "$note.subject"}} , { "$group" : { "_id" : "$emailId" , "type" : { "$first" : "$aType"} , "subject" : { "$first" : "$subject"}}}],"cursor": {"batchSize":10}}
Sandeep
  • 233
  • 4
  • 13