Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
Insert data into mongodb collection

I want to insert data for whole current month i.e. November every day of this month and every hour of each day and every minute of each hour and every second of each minute. I tried the following code which I tried for one day.

const data = [
    {
      'name': 'Name 10'
    }, {
      'name': 'Name 9'
    }, {
      'name': 'Name 8'
    }, {
      'name': 'Name 7'
    }, {
      'name': 'Name 6'
    }, {
      'name': 'Name 5'
    }, {
      'name': 'Name 4'
    },
    {
      'name': 'Name 3'
    },
    {
      'name': 'Name 2'
    },
    {
      'name': 'Name 1'
    }
];

const HOURS = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
const MINUTES = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59];
    for (let i = 0; i < data.length; i++) {
        for (let h = 0; h < HOURS.length; h++) {
            const hour = new Date().setHours(HOURS[h]);
            for (let m = 0; m < MINUTES.length; m++) {
                const n = data[i].name;
                const value =  Math.floor(Math.random() * (10 - 0 + 1)) + 0;
                const timestamp = new Date(hour).setMinutes(MINUTES[m]);
                new MongoCollection({ 'name': n, 'value': value, 'timestamp': timestamp }).save();
            }
        }       
    }

Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Why do you create arrays and use HOURS.length and MINUTES.length? for (let h = 0; h < 24; h++) and for (let m = 0; m < 60; m++) does the same.

What is the purpose of - 0 and + 0?

Inserting the documents one-by-one will let to serious performance issues.

Anyway, I would suggest moment.js library, would be similar to this.

for (let d of data) {
   var startTime = moment().startOf('month');
   var endTime = moment().endOf('month');
   var docs = [];
   while (startTime.isBeforeOrSame(endTime)) {
      docs.push({ 
          name: d.name, 
          value: Math.floor(Math.random() * 11), 
          timestamp: startTime.toDate() 
      });
      startTime.add(1, 'second');
   }
   db.collection.insertMany(docs);
}

Or maybe put array docs even at top level.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!