Say, I have 2 collections. Users and Orders as below: Users
{
id: '01',
name: 'john'
},
{
id: '02',
name: 'donald'
}
Orders
{
id: '01',
userId: '01'
},
{
id: '02',
userId: '02'
},
{
id: '03',
userId: '01'
}
I want to match all the Users that have more than 1 Order. I'm using localField, foreignField later in the pipeline. Example code being used:
db.Users.aggregate([
{
$match: {
activated: true
}
},
{
$sort: {
Date: -1
}
}
])
I want to filter some documents of Users collection based on the $lookup data. E.g $lookup: {from: 'Orders', localField: 'id', foreignField: 'userId', as: 'orders'}. how do I exclude documents from the aggregation that have fewer than 2 orders?