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

192
Views
Using lookup in mongodb to write a union query

Can someone please help me to make a union query:

I have all of the jobs that were created between a time period.

[{
    _id: '61be471835364400005ee66a',
    created: '2021-12-18T20:39:52.279Z',
    company: '61be44cb8c18cd0000be2048'
  }]

I got this doing an aggregate query:

  const matchByUserStage = {
    $match: {
      user: session?.user?.email,
      created: {
        $lte: new Date(end),
        $gte: new Date(start),
      },
    },
  };



const projectStage = {
    $project: {
      _id: {
        $toString: '$_id',
      },
      created: { $toString: '$created' },
      company: { $toString: '$company' },
    },
  };

I have a collection of companies, and am trying to get the COMPANY name to be pulled over into the query, rather than having to get it client side. I have the _id already in the aggregate query result. I have been trying to do this:

 const jobs = await Job.aggregate([
    matchByUserStage, projectStage,
    {$lookup: {
    from: 'Company',
    localField: 'company',
    foreignField: '_id',
    as: 'result'
    };
]);

I was thinking that this would 1) get the Company that has an _id that matches our 'company' object ID above and 2) save that in the result field. There is nothing there, despite the object id being a match! Could someone please help walk me through how this should work? I could do it very easily I think in SQL.

Company Schema is:

const CompanySchema = new mongoose.Schema({
name: String,
  domain: { protocol: String, url: String },
  logo: String,
  location: String,
  remote: String,
  user: {
    required: true,
    type: String,
  },
  orb_name: String,
  orb_num: Number,
  orb_address: {
    address1: String,
    address2: String,
    city: String,
    state: String,
    zip: String,
    country: String,
  },
  industry: String,
  categories: [{ name: String, weight: Number }],
  employees_range: String,
  year_founded: Number,
  description: String,
  created: { type: String, required: true },
});

and an example Company item is:

{ _id: 61be44cb8c18cd0000be2048,
    created: 'Sat Dec 18 2021 12:30:03 GMT-0800',
    user: 'scooter@scoot.com',
    name: 'Facebook',
    remote: 'friendly',
    location: '1601 WILLOW RD MENLO PARK  Menlo Park CA 94025 United States US',
    logo: 'https://logo.clearbit.com/facebook.com',}
about 4 years ago · Juan Pablo Isaza
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!