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

549
Vistas
Double Lookup and Unwind with .NET MongoDb Driver

I am trying to transpose a MongoDb query into a C# Mongo Driver query. The purpose is to make a join between collections. I mention that in this case, nesting documents isn't an option.

So the following MongoDb query works as expected:

db.Orders.aggregate(
    [ {$lookup: {
        from: 'Status',
        localField: 'orderStatusId',
        foreignField: '_id',
        as: 'status'
      }}, {$lookup: {
        from: 'PaymentTypes',
        localField: 'paymentTypeId',
        foreignField: '_id',
        as: 'paymentType'
      }}, {$unwind: {
        path: "$status"
      }}, {$unwind: {
        path: "$paymentType",
      }}]
)

However in C# it never finds any document:

      var orders = await _ordersCollection.Aggregate()
                .Lookup(_statusCollection, order => order.StatusId, status => status.Id, (Order order) => order.Status)
                .Lookup(_paymentTypeCollection, order => order.PaymentTypeId, paymentType => paymentType.Id, (Order order) => order.PaymentType)
                .Unwind(order => order.Status)
                .Unwind(order => order["paymentType"])
                .ToListAsync();

I have the following objects:

    public class Order
    {
        [BsonId(IdGenerator = typeof(GuidGenerator))]
        public Guid Id { get; set; }

        [BsonElement("status")]
        [BsonIgnoreIfNull]
        public Status Status { get; set; }

        [BsonElement("paymentType")]
        [BsonIgnoreIfNull]
        public PaymentType PaymentType { get; set; }

        [BsonElement("paymentTypeId")]
        public Guid PaymentTypeId { get; set; }

        [BsonElement("orderStatusId")]
        public Guid StatusId { get; set; }

    }

    public class PaymentType
    {
        [BsonId(IdGenerator = typeof(GuidGenerator))]
        public Guid Id { get; set; }

        [BsonElement("name")]
        public string Name { get; set; }
    }

    public class Status
    {
        [BsonId(IdGenerator = typeof(GuidGenerator))]
        public Guid Id { get; set; }

        [BsonElement("name")]
        public string Name { get; set; }
    }
over 4 years ago · Santiago Trujillo
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