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',}