Ghost
Ghost

Reputation: 326

How i get an array containing the value of mongodb field every 15 minutes

i am working on node js mongodb project , i have a collection named shop_infos which containing documents like this :

{
"_id": ObjectId("80c0ba30f2ed8e3d886965c4")
"shopId":ObjectId("5e9f0d3418c486041c6f5e8a")
"isOpen": true,
"created_at": 2021-06-09T09:00:12.458+00:00
"updated_at": 2021-06-09T09:00:12.458+00:00

}
{
"_id": ObjectId("80c0ba30f2ed8e3d886965c4")
"shopId":ObjectId("5e9f0d3418c486041c6f5e8a")
"isOpen": false,
"created_at": 2021-06-09T11:00:12.458+00:00
"updated_at": 2021-06-09T11:00:12.458+00:00
}

so this collection contains the shop infos (is open or not ) every time the shop is opened or closed a document is inserted to this collection containing the shopId and is open or not . What i want to do is to see the status of every shop every 15 minutes from 9:00 to 12:00 in one chosen day . The outpout i want to get is this

"shopId", "date", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15","10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00"
 5e9f0d3418c486041c6f5e8a, 09/06/2021 ; true, true , true , true, true, true, true, true, false, false, false, false
 5e9f0d3418c486041c6f5efd, 09/06/2021 ; true, true , true , true, true, true, true, true, true, true , true , true   

So here the day i chose is 09/06/2021 , i can do it in $match query ,in the first line of the array the shop have two infos document 1- its opened at 9:00 2- it is closed at 11:00 so between 9 and 11 the field isOpen is true every 15 minutes then from 11 to 12 it is false the second example is random one to tell you that i should also group the shops by id I've been trying a lot to accomplish that with mongodb aggregation but i am not even close to get the right result so i begin to wonder if this can be done with aggregation if no how can i do it with node js ?

Upvotes: 0

Views: 52

Answers (1)

Arpan Kc
Arpan Kc

Reputation: 938

Based on your requirements, one option can be:

  • Use a cron job (https://cron-job.org/en/) that triggers an endpoint in your application. The endpoint will query the database to see if the shop is open at that particular time. Cron job gives you the flexibility to schedule the cron job at a particular interval in the day, which in your case is 9 - 2 and also specify that it gets triggered every 15 minutes. enter image description here

If you want to aggregate all the results, you will need to further store all the separate query results in persistent memory and aggregate them afterwards.

Upvotes: 1

Related Questions