I've recently been using the aggregate
method in Mongo DB to join two tables based on a common field, however, have hit a brick wall with trying to query specific information in the joined results. It seems that I need to use the pipeline
option, but traditionally, I'd use $query
to pull out field.
My JS is:
const results = await client.db().collection('data_source_one').aggregate([{
$lookup: {
from: 'data_source_two',
// localField: 'created_at',
// foreignField: 'created_at',
pipeline: [{
$match: {
$expr: {
$and: [
{ $exists: ['$data.sessions'] }
]
}
}
}],
as: 'combined_results'
}
}]).toArray();
Now, data_source_two
returns an object called data
which has a field in there called sessions
, and I want to do several checks, the first is to make sure that it exists and also make sure that it equals a certain value.
But it seems like I also have to drop the check for localField
and foreignField
?
Where am I going wrong? Can I just pass $query
as another object into the aggregate
method?
question from:
https://stackoverflow.com/questions/65916015/querying-fields-in-mongo-db-joined-collection-with-aggregate 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…