A
A
Alexander Buki2019-02-05 10:55:21
MongoDB
Alexander Buki, 2019-02-05 10:55:21

How to get a list of all users in the last hour from Mongo db?

There is a DB on mongo.
The scheme is this:

let reportSchema = new Schema({
 ssoid: {type: String},
 ts: {type: Date},
 grp: {type: String},
 type: {type: String},
 subtype: {type: String},
 url: {type: String},
 orgid: {type: String},
 formid: {type: String},
 code: {type: String},
 ltpa: {type: String},
 sudirresponse: {type: String},
 ymdh: {type: String}
 });

Example entries:
{"_id":{"$oid":"5c58af318c5faf0f20775db6"},"ssoid":"650ae77a-ffce-475d-a930-c7e345e0658c","ts":{"$date":{"$numberLong":"1499763594000"}},"grp":"guis_-47","type":"formcontroller","subtype":"send","url":"https://www.mos.ru/pgu/ru/application/guis/-47/#step_1","orgid":"guis","formid":"-47","code":"MPGU","ltpa":"","sudirresponse":"","ymdh":"2017-07-11-09","__v":{"$numberInt":"0"}}

 {"_id":{"$oid":"5c58af318c5faf0f20775db7"},"ssoid":"aeadac26-3e19-438b-8e4a-a1ae754bbe41","ts":{"$date":{"$numberLong":"1499763600000"}},"grp":"guis-47","type":"bill","subtype":"prepare","url":"","orgid":"","formid":"","code":"","ltpa":"","sudirresponse":"","ymdh":"2017-07-11-09","__v":{"$numberInt":"0"}}

I need to get a list of all ssoid (users) for the last hour (ts field)
I do this, but it's not at all the same:
let Report = mongoose.model('Report', reportSchema);



 Report.aggregate([
     { $sort: { ts: 1 } },
    { $group: {_id:'$ssoid', count:{$sum:1}, lastSalesDate: { $last: "$ts" }}},
]).
then(function (res) {
    console.log(res); 
});

As I understand it, you first need to get a record with the maximum time, and then subtract an hour from it and already a filter by range

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
forspamonly2, 2019-02-06
@forspamonly2

"in the last hour" is "greater than the current time minus 3600 seconds"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question