These are chat archives for Automattic/mongoose

6th
Oct 2017
Vlado Tesanovic
@vladotesanovic
Oct 06 2017 07:38
@jemliF Select object in pre hook and compare it with passed data.
Nishanth Shetty
@linknissan
Oct 06 2017 12:35
How Do I run Aggregation in MongoDb using MMVAP1 Engine ? This is working fine in Wired Tiger .
Func looks smth like this
var start_trip_date = new Date(‘2017-10-02T12:01:00.000Z’)
var end_trip_date = new Date()
var vehicle_lookup = { from: ‘collections’, localField: ‘vehicle_id’, foreignField: “_id”, as: “vehicle”}
var customer_lookup = { from: ‘collections’, localField: ‘vehicle.customer_id’, foreignField: “_id”, as: “customer” }
var contract_lookup = { from: ‘collections’, localField: ‘customer.customer_contract_id’, foreignField: “_id”, as: “contract” }
db.getCollection(‘collections’).aggregate([
{ $lookup: vehicle_lookup },
{$unwind: “$vehicle”},
{ $lookup: customer_lookup },
{$unwind: “$customer”},
{ $lookup: contract_lookup },
{$unwind: “$contract”},
{ $match: { kind: ‘Trip’ ,vehicle_id: ObjectId(‘59cca81e15d4c521e676a8b6’), start_trip: { $ne: null } } },
{ $group: {
_id : “$vehicle_id”,
start_trip: { $first: ‘$start_trip’ },
total_km: { $sum: “$total_km” },
vehicle: { $first: { type: “$vehicle.truck_type”,vehicle_number: “$vehicle.vehicle_number”,customer_id: “$vehicle.customer_id” } },
customer: { $first: { customer_id: “$customer._id”,contract_id: “$customer.customer_contract_id” } },
contract: { $first: {
extra_hours_rate: “$contract.extra_hr_rate”,
extra_km_rate: “$contract.extra_km_rate”,
working_hours: “$contract.work_hours”,
working_days: “$contract.work_days”,
commit_km: “$contract.commit_km”
}
},
served: { $sum: 1 }
}
},
{$addFields: { extra_km: { $subtract: [“$total_km”,“$contract.commit_km”] } }}
])