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

121
Views
How to convert authorid into postid so that i can extract author details and store in my authorDocument array?

**Why this code isn't working? **

let posts = await postsCollection.aggregate([
  {$match: {_id: new ObjectID(id)}},
  {$addFields: {authorId: { $toObjectId: "$author"}}},
  {$lookup: {from: "users", localField: "author", foreignField: "_id", as: "authorDocument"}}
]).toArray()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Below should work.. I presume that postsCollection and users are the collection you want to join based on authorId as common key but its stored as string in postsCollection and as _id in users.

let posts = await postsCollection.aggregate([
  {$match: {_id: new ObjectID(id)}},
  {$addFields: {authorId: { $toObjectId: "$author"}}},
  {$lookup: {from: "users", localField: "authorId", foreignField: "_id", as: "authorDocument"}}
]).toArray()

Note: you have used author but it should be authorId

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!