IsaacLevon
IsaacLevon

Reputation: 2570

MongoDB aggregation: How to flatten an object?

I have the following document:

{
  value: {
   a: 1,
   b: 'yellow',
   c: {'foo': 'baz'}
  }
}

How can I transform it to

{
   a: 1,
   b: 'yellow',
   c: {'foo': 'baz'}
}

Note: I don't want to project each and every field manually. I'm looking for an automatic way to do it

Thanks!

Upvotes: 1

Views: 504

Answers (1)

Rubén Vega
Rubén Vega

Reputation: 730

I think you are looking for $replaceRoot. Try this playground.

 {
    "$replaceRoot": {
      "newRoot": "$value"
    }
  }

More info in docs.

Upvotes: 3

Related Questions