0

What would be the equivalent of " $push: ‘$$ROOT’ " command in an aggregate query, when using the Mongo Csharp driver ? What would be the c# equivalent of the following json query (containing $push: ‘$$ROOT’) when using the mongodb csharp driver, with the fluent aggregate or linq (eventually using the new Linq3 provider) 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 } }
])
Abra Sat
  • 11
  • 2
  • C# driver allows specifying arbitrary valid MQL stages. See [here](https://stackoverflow.com/questions/68395490/how-to-use-raw-mongodb-aggregation-query-in-c/68397480#68397480). – dododo Dec 20 '21 at 11:38
  • Thanks for the information. I was looking mainly for a type-safe solution, without having to use json parts in the aggregation query. Is there no linq/fluent aggregation c# solution possible for the above query, without the partial usage of untyped BsonDocuments? – Abra Sat Dec 20 '21 at 12:19
  • linq2 doesn't support it, you can look at recently released linq3: http://mongodb.github.io/mongo-csharp-driver/2.14/reference/driver/crud/linq3, I see some tests with `$$ROOT` in the output: https://github.com/mongodb/mongo-csharp-driver/blob/8278e2b304092d67dce66dd1dc0bbf5a41fd467d/tests/MongoDB.Driver.Tests/Linq/Linq2ImplementationTestsOnLinq3/MongoQueryableTests.cs#L460, but I didn't try it. – dododo Dec 20 '21 at 13:01

0 Answers0