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

337
Vistas
How to get values of one field in array in MongoDB Driver for C#

I have the classes:

public class Whole
{
  public ObjectId Id { get; set; }
  public string NeededField { get; set; }
  public List<Detail> Details { get; set; }
  public string SomeUnnecesaryField { get; set; } 
}

public class Detail
{
  public string NeededField { get; set; }
  public string NotNeededField { get; set; }
}

[BsonNoId]
[BsonIgnoreExtraElements]
public class MyNeededInformations
{
  public string NeededField { get; set; }
  [BsonElement("Details.NeededField")]
  public List<string> DetailsNeededFields { get; set; }
}

And I trying to obtain it with projection:

var filter = someFilter;
var projection = Builders<Whole>.Projection
  .Include(w => w.NeededField)
  .Include(w => w.Details)
  .Exclude("_id");
return Collection
  .Find(filter)
  .Project(projection)
  .As<MyNeededInformations>()
  .ToList();

And I receive DetailsNeededFields as empty list every time. I want to have list of strings like with standard aggeregate:

db.collection.aggregate([
  {$match: someFilter},
  {$project: {"_id": 0, "NeededField": 1, "DetailsNeededFields": "$Details.NeededField"}}
])
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

can be achieved easily with the AsQueryable interface like so:

var results = await collection
    .AsQueryable()
    .Where(_ => true)
    .Select(w => new MyNeededInformations
    {
        NeededField = w.NeededField,
        DetailsNeededFields = (List<string>)w.Details.Select(d => d.NeededField)
    })
    .ToListAsync();
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