I have used aggregate function.
我用了聚合函数。
db.checkins.aggregate([
{$match: {checkinType: "Beacon",
"associationIds.organizationId":"af39bc69-1938-4149",
"checkinData.time": {"$gte": new Date("2018-01-18T18:30:00.000Z"),
"$lt": new Date("2018-01-19T18:30:00.000Z")}
}
},
{"$sort":{"checkinData.time":-1}},
{$group: {"_id":
{"orgId":"$asst.organizationId", "userId":"$asst.userId"},
"lastSeen":{"$first":"$checkinData.time"},
"firstSeen":{"$last":"$checkinData.time"},
}
},
{"$project":{"_id":1,"lastSeen":1, "firstSeen":1,
totalHourSpent:{$subtract: ["$lastSeen","$firstSeen"]}}},
])
db