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

147
Vistas
How do I return a document with filtered sub-documents using Mongo.Driver.Linq with the Mongo C# driver 2.3?

Given the following, how do I return all the Foos that contain Bars that have TypeOfBar == "Big" and have the Foos' Bars be limited to only those Bars that have TypeOfBar == "Big" as well?

public class Foo
{
    public string _id { get; set; }
    public List<Bar> Bars { get; set; }
}

public class Bar
{
    public string _id { get; set; }
    public string TypeOfBar { get; set; }
}

I can easily get the first part (all Foos with Bars of a particular type):

var client = new MongoClient("myconnectionstring");
var db = client.GetDatabase("myDb");
var collection = db.GetCollection<Foo>("Foos");

var foos = collection.AsQueryable().Where(x => x.Bars.Any(b => b.TypeOfBar == "Big"));

However, I'm having a hard time figuring out how to also have the db return the Foos with a filtered list of Bars.

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

0

If you want to filter out only this Foos that have type "Big" you should apply ElemMatch Projection:

var res = collection.Find(x => x.Bars.Any(b => b.TypeOfBar == "Big"))
    .Project(Builders<Foo>.Projection.ElemMatch(x=>x.Bars, b=>b.TypeOfBar == "Big"));

The problem you will have with it: Projection will return BSon. Perhaps that is what you need and you could live with it, if not, you shuold deserialize Bson to your Foo class. Complete query i did look like:

var res = collection.Find(x => x.Bars.Any(b => b.TypeOfBar == "Big"))
    .Project(Builders<Foo>.Projection.ElemMatch(x=>x.Bars, b=>b.TypeOfBar == "Big"))
    .ToEnumerable()
    .Select(b=>BsonSerializer.Deserialize<Foo>(b))
    .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