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

605
Views
¿Cómo convierto mi consulta de búsqueda de mongoose en el marco de agregación de mongoDB?

Quiero convertir mi código existente al marco de agregación de mongoDB. Aquí está mi código que quiero convertir en agregación.

 const comments = await Comments.find({post:req.params.id}) .populate({ path:"comments", populate:{ path:"author", select:"name photo" }, select:{ createdAt:1, author:1, _id:1, body:1, likes:1 } }) .populate("author","photo name") .select({ createdAt:1, author:1, _id:1, body:1, comments:1, likes:1 }) .sort("-createdAt");

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede usar la siguiente agregación

 Comments.aggregate([ { "$match": { "post": mongoose.Types.ObjectId(req.params.id) } }, { "$sort": { "createdAt": -1 } }, { "$lookup": { "from": Comment.collection.name, "let": { "comments": "$comments" }, "pipeline": [ { "$match": { "$expr": { "$in": ["$_id", "$$comments"] } } }, { "$lookup": { "from": Author.collection.name, "let": { "author": "$author" }, "pipeline": [{ "$match": { "$expr": { "$eq": ["$_id", "$$author"] } } }, { "$project": { "name": 1, "photo": 1 } }], "as": "author" } }, { "$unwind": "$author" }, { "$project": { "createdAt": 1, "author": 1, "_id": 1, "body": 1, "likes": 1 } } ], "as": "comments" } }, { "$lookup": { "from": Author.collection.name, "let": { "author": "$author" }, "pipeline": [{ "$match": { "$expr": { "$eq": ["$_id", "$$author"] } } }, { "$project": { "name": 1, "photo": 1 } }], "as": "author" } }, { "$unwind": "$author" }, { "$project": { "createdAt": 1, "author": 1, "body": 1, "comments": 1, "likes": 1 } } ])
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!