Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!