How to get latest and complete document?

I am trying to use aggregate to get latest and complete document , but my subDocument is not incomplete.

this is my document list example.

[
{
    "t": {
        "$date": "2021-01-01T00:00:01.000Z"
    },
    "id": "1234",
    "a": {
        "y": "y1",
        "x": "x1"
    },
    "b": {
        "c": "c1",
        "d": {
            "dd":"dd1" 
        }
    }
},
{
    "t": {
        "$date": "2021-01-01T00:00:02.000Z"
    },
    "id": "1234",
    "a": {
        "y": "y2"
    },
    "b": {
        "c": "c1",
        "d": {
            "ee":"ee1" 
        }
    }
},
{
    "t": {
        "$date": "2021-01-01T00:00:03.000Z"
    },
    "id": "1234",
    "b": {
        "d": {
            "ee":"ee2" 
        }
    }
]

And I expected result is

[{
    "id": "1234",
    "a": {
        "x": "x1",
        "y": "y2"
    },
    "b": {
        "c": "c1",
        "d": {
            "dd":"dd1" ,
            "ee":"ee2" 
        }
    }
}]

So I try to use aggregate like this

db.collection.aggregate([
  {
    $sort: {
      "t": 1
    }
  },
  {
    $match: {
      "id": "1234"
    }
  },
  {
    $group: {
      _id: null,
      result: {
        $mergeObjects: "$$ROOT"
      }
    }
  }
])

But actual result is this , because aggregate just get first level keys.

[
  {
    "_id": null,
    "result": {
      "_id": ObjectId("5a934e000102030405000002"),
      "a": {
        "y": "y2"
      },
      "b": {
        "d": {
          "ee": "ee2"
        }
      },
      "id": "1234",
      "t": ISODate("2021-01-01T00:00:03Z")
    }
  }
]

So How to meet my expectations?
I have no idea.
Thanks!!

Hi @_Steven,

Welcome to MongpDB Community.

This behaviour is expected as mergeObjects can only merge the top level fields and cannot understand how to merge low level nested ones.

If you need to merge them you need to do each one separately:

[{$match: {
  id: '1234'
}}, {$sort: {
  t: 1
}}, 
{$group: {
  _id: null,
 a: {$mergeObjects : "$a"},
 b : { $mergeObjects : "$b" },
 d : { $mergeObjects : "$b.d" },
 top : { $mergeObjects : "$$ROOT"}
}},
 {$project: {
  _id : "$top._id",
  t : "$top.t",
  id : "$top.id",
  a : 1,
  b : {
    c :1,
    d : "$d"
  }
}}]  

Thanks,
Pavel