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

125
Views
Filtrar campo poblado profundo en MongoDB

Cómo filtrar productos por campos poblados profundamente anidados. catalogProduct es un ObjectId (ref to catalog product). categoría es un ObjectId dentro de catalogProduct (referencia a categorías). Categorías es una matriz de identificadores de categoría.

 products = await StorageModel .find({"catalogProduct.category": {$in: categories }}) .skip((page-1)*8) .limit(8) .populate({path: "catalogProduct", populate: {path: "category", select: "name"}}) .select('-__v') .sort({_id: -1});

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Deberá realizar una $lookup en la colección catelogProduct para poder acceder a los datos de catelogProduct en la consulta.

Desafortunadamente, eso solo está disponible cuando se usa Mongo Aggregation; sin embargo, la agregación es muy poderosa y es perfecta para este tipo de cosas. Podrías hacer algo como esto:

 const products = await StorageModel.aggregate([ { $lookup: { // Replace the Catelog Product ID with the Catelog Product from: "catelogProduct", localField: "catelogProduct", foreignField: "_id", as: "catelogProduct" } }, { $lookup: { // Replace the Category ID with the Category from: "categories", localField: "catelogProduct.category", foreignField: "_id", as: "catelogProduct.category" } }, { $addFields: { // Replace the Category with its name "catelogProduct.category": "$catelogProduct.category.name" } }, { $match: { "catalogProduct.category": { $in: categories } } }, { $sort: { _id: -1 } }, { $skip: (page - 1) * 8 }, { $limit: 8 } ]);

Idealmente, no haría la $lookup hasta que haya paginado los resultados (usando $skip y $limit ), pero en este caso tiene sentido hacer la $lookup primero. Asegúrese de tener un índice en catelogProduct._id y categories._id para optimizar la consulta.

Para obtener más información sobre $lookup , consulte este artículo. Para obtener más información sobre Mongo Aggregation, consulte este artículo.

about 4 years ago · Juan Pablo Isaza 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!