Considering the following documents:
[{
"imei": "352413080100000",
"datestamp": "2019-03-25T19:17:11.471Z"
}]
How can I get a unique list of all imei where there is no record for the past hour?
You can use $group
aggregation to find the unique record for the imei
field and use moment
library to cut of the last hour from the current time.
db.collection.aggregate([
{ "$match": {
"datestamp": {
"$lte": moment().subtract(1, 'hours').toDate()
}
}},
{ "$group": {
"_id": '$imei'
}}
])
Or even you can use distinct query as well
const imeis = await db.collection.distinct('imei', { "datestamp": { "$lte": moment().subtract(1, 'hours').toDate() } })
console.log(imeis)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments