Calculate the count as per time difference using aggregate function in MongoDB
NickName:satya Ask DateTime:2019-08-29T19:23:04

Calculate the count as per time difference using aggregate function in MongoDB

I need to calculate the no of records as per some time interval using MongoDB aggregate function. I am showing my collection below.

allocation:

state1                 state2                              createdDate

test1                  test2                     2019-08-16T09:41:22.202Z

test1                  test2                     2019-08-16T09:41:22.202Z

test3                  test4                     2019-08-16T09:41:22.202Z

test1                  test2                     2019-08-16T09:41:22.202Z                     

In my allocation collection I have state1,state2,createdDate column and here I need to calculate the no of record within some time interval as per user input (i.e-test1 & test2). Here Using aggregate function I need to the result like below.

difference                  count

0-6HRs                        2
6-12HRS                       0

................................
................................

The time difference will calculate from createdDate column value.

Copyright Notice:Content Author:「satya」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57709087/calculate-the-count-as-per-time-difference-using-aggregate-function-in-mongodb

More about “Calculate the count as per time difference using aggregate function in MongoDB” related questions

Calculate the count as per time difference using aggregate function in MongoDB

I need to calculate the no of records as per some time interval using MongoDB aggregate function. I am showing my collection below. allocation: state1 state2 ...

Show Detail

MongoDB - Aggregate by distinct field then count per day

I have a mongodb database that collects device data. Example document is { "_id" : ObjectId("5c125a185dea1b0252c5352"), "time" : ISODate("2018-12-13T15:09:42.536Z"), "mac&q

Show Detail

MONGODB Aggregate Function

I am trying to calculate the average of ratings corresponding to doctor's id using MongoDB query(Version 3.2) doctorsReview.aggregate( { $group: { _id: "doctors_id...

Show Detail

difference between aggregate ($match) and find, in MongoDB?

What is the difference between the $match operator used inside the aggregate function and the regular find in Mongodb? Why doesn't the find function allow renaming the field names like the aggreg...

Show Detail

calculate distance using aggregate mongodb

i have a problem with calculate distance. i have data in database like this : longlat: [-6.117223545020398, 106.72738171204378]. in there, i'am using mongodb. and i using package geolib to get

Show Detail

MongoDB C# Aggregate Count

I wish to use the C# Driver (V 2.7.0) to aggregate on a collection and then count the results. Assuming I have an IMongoCollection col, I do the aggregate like this: IAsyncCursor<BsonDocument&...

Show Detail

Count of the result of a MongoDB aggregate query

After I write a MongoDB aggregate command, I would like to quickly look at the number of results it returned. Say, my query is something like this: (probably more complex) db.zipcodes.aggregate(...

Show Detail

MongoDB query for aggregate function retuning empty result

Following is the query i had written to get the count based on event name. $data= DB::collection(Events::$collection)->raw(function($collection) { return $collection->aggregate([ ['$...

Show Detail

Aggregate query on time in MongoDB

I'm using MongoDB Aggregate pipeline and for most of the things I want to do, it's perfect so far. However, I'd like to aggregate on time and group by individual days, hours or minutes. This is my...

Show Detail

Using Featuretools to aggregate per time time of day

I'm wondering if there's any way to calculate all the same variables I already am using deep feature synthesis (ie counts, sums, mean, etc) for different time segments within a day? I.e. count of

Show Detail