Get and merge all the documents in MongoDB which were inserted in the last minute by using Node.js

Brutal Jazzy

I am trying to get the documents which were inserted into MongoDB in the last minute only and once i get them i am trying to merge them as one and insert them into a new Collection.
For example: These are my documents in "Prices" Collection:

{_id: someID, timeStamp: 1629131116370, name: Jazzy},   
{_id: someID, timeStamp: 1629131117370, name: John}, 
{_id: someID, timeStamp: 1629151191370, name: David},  
{_id: someID, timeStamp: 1629151192370, name: Julie}, 
{_id: someID, timeStamp: 1629151193370, name: Hobit}, 

Now what i want to do is create a function which gets all the documents of the last minute let say in my case it is the last three documents i.e (David, Julie, Hobit) and merge into one and also shows which minute was started and when it ended in utc timestamp milliseconds only.

{_id: newSomeID, 
minuteStarted: 1629151140000, <----- Please keep in mind this is the start of that minute.
minuteEnded: 1629151200000, <----- Please keep in mind this is the end of that minute.
allDataofthisminute: [
{_id: someID, timeStamp: 1629151191370, name: David},
{_id: someID, timeStamp: 1629151192370, name: Julie},
{_id: someID, timeStamp: 1629151193370, name: Hobit}
 ]
}

What i have Tried:
I surely know that it is a complex logic to make. what i tried is first get the current timestamp (in milliseconds) and minus the milliseconds which is passed from this minute and i got the last minute ended timestamp. Then i subtracted 60,000 from the value and i got the last minute started timestamp.
now i have the last minute started timestamp and minute ended timestamp and i tried many things but all in vain. I know this is too much to ask but i just want to understand things more clearly.

var currentTimeStamp = new Date().getTime();
var currentMilliSeconds = Date.now() % 60000;
var lastMinuteStarted = currentTimeStamp - currentMilliSeconds - 60000;
var lastMinuteEnded = currentTimeStamp - currentMilliSeconds;

Now how should i place a $and query to get all documents range within the lastMinuteStarted and lastMinuteEnded. i hope i have explained well. Thanks

Brutal Jazzy

Update: I solved it with big aggregation pipeline. if someone improves it or has better suggestion please you are more then welcome to help me out.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MongoDB get the number of documents (count) in a collection using Node.js

Mongodb aggregation - get all last inserted group of docs based on time

Get id of last inserted document in laravel after save() using mongodb

To get last inserted Id from mongo db in node.js

Get all data from Collection using mongodb and node js

django Get Distinct rows which are inserted last

GET all documents from 3 collections in mongoDB API Node Express

How to get all ids to pouchdb's documents using node.js

Get last inserted document with tag MongoDB Java

Java MongoDB FindOne to get last inserted record

Get last inserted element from mongodb in GoLang

Datastore get last inserted id with Node

How to get documents that were updated in the last hour from firestore?

Uploading CSV files as MongoDB documents using node.js

How to get all children documents using $graphLookup of MongoDB

get all the documents having max value using aggregation in mongodb

How to update _id of all documents in a collection in mongo using node js?

Insert Hour and Minute to mongoDB date in node js

How to get all documents which are created in last three months in mongo shell

Get the count of the times date is inserted to know the last for loop iteration in Node.js

Using GroupBy in LINQ to get last inserted rows

How to get last inserted id using AJAX?

How to get Last inserted id using Nodejs

Get the Last Inserted Id Using Laravel Eloquent

MongoDB - Get documents by their last embedded document

Get documents which expired before today in MongoDB

View last N documents using MongoDB Compass

Fetching last record from mongodb using node.js

Select Last N documents of all Users in MongoDB Collection using C#