$push: '$$ROOT' equivalent in Mongo Csharp driver

I have a json aggregation query that I would like to convert into c# using the linq or aggregate fluent syntax:

db.getCollection('customers').aggregate(
  [
    { $match:
        { $and:
	 [
          { RegistrationDate: { $lte: ISODate('2021-12-13T23:59:59.999Z'), $gte: ISODate('2011-11-22T00:00:00.0Z') }},
          { $or:
	    [
	      { CountryOfResidence: 'France'},
	      { CountryOfResidence: 'Spain'},
	    ]
	  },
          { Age: {$gte: '40'}}
	 ]
	}
    },
    { $group: { _id: '$LastName', count: { $sum: 1 }, data: { $push: '$$ROOT' } } },
    { $match: { count: { $gte: 2 } } }, { $project: { count: 0 } }
  ])

Can somebody show me please what is the equivalent of the above json query containing $push: ‘$$ROOT’ when using the c# driver?

1 Like

As I tested today with the new version of C# driver (2.24.0), This has been supported recently and was not supported in the previous versions.

var result = await customersCollection.Aggregate()
    .Match(....)
    .Group(c => c.LastName, g => new { Count = g.Count(), Data = g.ToList() })
    .Match(....)
    .ToListAsync()