Reputation: 1
I am trying for a query with lookup crops (with fields, cropid, cycleStartDate) and harvest (with fields harvestDetails.harvestDate, harvestDetails.pieces).
Kindly help me in the lookup when its match cropid and doc (doc should be match with subtract value)
db.getCollection('dailyrecords').aggregate(
[
{ $match: { deleted: false } },
{
$lookup: {
as: 'crops',
from: 'crops',
foreignField: 'cropId',
localField: 'cropId'
}
},
{
$match: {
'crops.deleted': false,
'crops.stockingDetails.species': {
$in: ['Vannamei', 'Monodon']
}
}
},
{
$addFields: {
cid: { $toString: '$cropId' }
}
},
{
$lookup: {
from: 'harvests',
let: {
crop_id: '$cid',
crop_date: '$crops.cycleStartDate',
hrvstdate:
'$harvests.harvestDetails.harvestDate'
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ['$cropId', '$$crop_id']
},
{ $eq: ['$deleted', false] },
{
$gte: [
'$doc',
{
$trunc: {
$divide: [
{
$subtract: [
'$$hrvstdate',
'$$crop_date'
]
},
86400000
]
}
}
]
}
]
}
}
}
],
as: 'harvests'
}
},
{ $match: { cropId: 8124 } }
],
{ maxTimeMS: 60000, allowDiskUse: true }
);
I want to match the value between two field with subtract (lookup document value in an array)
Upvotes: 0
Views: 21