Question

I would like to perform a projection on a MongoDB document, which has a nested document. For example, say I have the following document:

Foo : {
      fooField1 : 1,
      fooField : 2,
      Bar : {
            barField : 1,
            barField : 2
      }
}

I would like to get the following back from my mongoTemplate.aggregate(..) call:

Foo : {
    fooField1 : 1,
    Bar : {
        barField1 : 1
    }
}

I cannot seem to figure out a how to do this with the MongoTemplate. I have tried many different combinations, including:

Aggregation.project(Fields.field("fooField1").and("Bar").project(Fields.field("barField1"))

I haven't had any luck so far. Does anyone have any suggestions?

Was it helpful?

Solution

You typically need to $unwind nested data in the aggregation framework

After you unwind Bar, match it to FooField

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top