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

162
Views
Cómo proyectar después de la búsqueda sin el campo mangosta 'como'

Tengo el siguiente código:

 const products: ReadonlyArray<Pick<IProduct, 'id' | 'gender' | 'category' | 'title' | 'description' | 'price' | 'imageFileName'>> = await UserDB.aggregate< Pick<IProduct, 'id' | 'gender' | 'category' | 'title' | 'description' | 'price' | 'imageFileName'>>([ { $match: { _id: mongoose.Types.ObjectId(req.userId) }, }, { $lookup: { from: 'products', localField: 'inBagProducts', foreignField: '_id', as: 'product', }, }, { $unwind: '$product', }, { $project: { product: { 'product.category': 1, 'product.gender': 1, 'product.title': 1, 'product.description': 1, 'product.price': 1, 'product.imageFileName': 1, } }, }, ]);

Este código da como resultado:

 { _id: 611e2febb863ce74ac448220, product: { category: 1, gender: 2, title: 'jdwoeid', description: 'jfwoeifjweoi', price: 23902, imageFileName: '1628808613195-7886.png' } }, { _id: 611e2febb863ce74ac448220, product: { category: 5, gender: 1, title: 'sivdosi', description: 'oisbdvoi', price: 2394, imageFileName: 'http://localhost:3000/images/1628875244435-3564.png' } }

El resultado esperado es este:

 { _id: 611e2febb863ce74ac448220, category: 1, gender: 2, title: 'jdwoeid', description: 'jfwoeifjweoi', price: 23902, imageFileName: '1628808613195-7886.png' }, { _id: 611e2febb863ce74ac448220, category: 5, gender: 1, title: 'sivdosi', description: 'oisbdvoi', price: 2394, imageFileName: 'http://localhost:3000/images/1628875244435-3564.png' }

¿Cómo puedo tener el resultado sin el product: {}

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

0

Puede usar la canalización $addFields para agregar _id a la propiedad del product y luego puede usar la canalización $replaceRoot . Puede agregar estas etapas de agregación a su código:

 [ { "$addFields": { "product._id": "$_id" } }, { "$replaceRoot": { "newRoot": "$product" } } ]

Aquí está el ejemplo de trabajo: https://mongoplayground.net/p/VVxXgDYFANI

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!