Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
What is the C# equivalent of push and root for MongoDB?

I have a collection of people. I am trying to find the oldest person for each name. I am able to achieve that result using the mongoDB console command

db.People.aggregate([
    { '$sort': { 'Name': 1, 'Age': -1 } }, 
    {       
        '$group': {
            '_id': '$Name',
            'docs': { '$push': '$$ROOT' },
        }
    },
    {
        '$project': {
            'top_one': { 
                '$slice': ['$docs', 1]
            }
        }
    } ])

What is the equivalent of this for the C# driver? I'm specifically having trouble with

'docs': { '$push': '$$ROOT' },

Here is my current C# query:

collection.Aggregate(aggArgs)
   .SortByDescending(x => x.Age) 
   .Group(x => x.Name, x => new { 
       Name = x.First().Name, 
       FavoriteColor = x.First().FavoriteColor, 
       FavoriteFood = x.First().FavoriteFood
    }).ToListAsync().Result;

It works close to what my mongo console command does, but I really don't like constructing an anonymous object. I would prefer to do Group(x => x.Name,x => x.First()), but that throws and exceptions that "First()" is not supported. I believe part of the problem is that is that my Person type doesn't have an ID, so _id is put on the actual mongo document (automatically by mongo on insert). When it trys to go back in to the type, it can't do a direct mapping.

So, with the two versions of the query in mind, how do I get my full type back in C# without having to spell out EVERY field?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It's a feature of MongoDB driver. It doesn't accept simple First(), it need something behind it. That was what i have seen by debugging. So you should either continue to work with First()... or you could query your json directly:

var result = collection.Aggregate()                
        .Group(new JsonProjectionDefinition<People>(@" {
              '_id': '$Name',
               'docs': { '$push': '$$ROOT' },}"))
        .Project<TopOne>(new JsonProjectionDefinition<BsonDocument>(@"{
            'top_one': { 
            '$slice': ['$docs', 1]
        } }"))
       .ToList();
over 4 years ago · Santiago Trujillo Denunciar

0

Maksim got me a lot of the way there. Here is what I ended up with in order to get the same types out as I was putting in:

collection.Aggregate(aggArgs)
  .SortByDescending(x => x.Age)
  .Group(new JsonProjectionDefinition<Person>(@"{
     '_id': '$Name',
     'docs': { '$push': '$$ROOT'}
    }"))
  .Project<BsonDocument>(new JsonProjectionDefinition<BsonDocument>(@"{
     'top': { '$slice': ['$docs', 1] }
    }"))
  .Unwind<BsonDocument>(new StringFieldDefinition<BsonDocument>("top"))
  .ReplaceRoot(new BsonValueAggregateExpressionDefinition<BsonDocument, BsonDocument>(@"$top"))
  .Project<Person>(new JsonProjectionDefinition<BsonDocument>(@"{
     _id: 0
    }"))
  .ToList();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda