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

146
Vistas
LINQ query to push an item to the array of a Mongo Document

I want to push a ProductPhoto inside the Product using LINQ. Here is my model:

public class Product
{
   public string Id { get; set; }
   public string Name { get; set; }
   public string Detail { get; set; }
   public List<ProductPhoto> ProductPhotos { get; set; }
}

public class ProductPhoto
{
   public string Id { get; set; }
   public string Url { get; set; }
}

How can I achieve this with the LINQ query? I am expecting the result to be something like this.

{
    _id: ObjectId('602d2149e773f2a3990b47f5'),
    Name: 'Some name',
    Detail: 'Some description',
    ProductPhotos: [
      { _id: ObjectId('602d2149e773f2a3990b47f5'), Url: 'Some URL' }
    ] 
}

Next time when I add a new ProductPhoto, then there will be two elements.

Thanks!

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

0

Here is the solution to similar question:

How to 'Find And Push' element in an array inside of another array in a document using LINQ to MongoDb

You can achieve this with something like this:

var filterBuilder = Builders<Product>.Filter;
var filter = filterBuilder.Eq(x => x.Id, productId);
var updateBuilder = Builders<Product>.Update;
var update = updateBuilder.Push(doc => doc.ProductPhotos, productPhoto);
Products.UpdateOneAsync(filter, update);
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