Tips for efficiently inserting 10,000 records into MongoDB with identical keys and values

I am currently focused on developing the server side functionality for a new game application.

This game will feature a spin wheel concept, and in order to implement this, I need to store a large amount of records. Specifically, I will need to store 10,000 records for 100 coins, 5,000 records for 200 coins, 100 records for 500 coins, as well as 1 record each for 1000 coins and 10,000 coins.

To handle this data storage, I plan to run a cron job at the end of each day to store documents and remove all records from the previous day.

When the spin wheel is activated, a random document will be selected from the available (unused) documents.

//Document Structure
{
    "No_of_Coins": 100,
    "position": 4,
    "Recorded_Spin": false,
    "date": new Date()
}

While I could use a for loop to insert the 10,000 records sequentially, I am looking for a faster and more efficient approach to implement this algorithm.

Answer №1

To populate your database with 10000 items, you can simply create an array containing all the items and then use the insertMany method to insert them.

const dataArray = new Array(10000).fill({
    "No_of_Coins": 100,
    "position": 4,
    "Recorded_Spin": false,
    "date": new Date()
})

db.collection.insertMany(dataArray);

Answer №2

function generateCoinsArray(coins, times, data) {
  return coins.flatMap((coin, index) => Array(data[index] || times).fill(coin));
}

var coinsData = [{
  "No_of_Coins": 100,
  "position": 4,
  "Recorded_Spin": false,
  "date": new Date()
}];

var resultArray = generateCoinsArray(coinsData, 1000, coinsData[0]);
console.log(resultArray);

Let's give this code a shot.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method to determine if an HTML attribute contains an absolute or relative URL when utilizing Protractor?

While testing a web application with Protractor, I encountered an issue where the href attribute is always treated as an absolute URL, making it difficult to verify if the rendered URL is actually absolute. For example, when trying to include the domain i ...

At what point does the chaining of async/await come to an end?

I was experimenting with node-fetch and encountered a question while using async / await: Do I need to make my function async if I use await in it? But then, since my function is async, I need to await it and make the parent function async. And so on... He ...

What is the process to install a npm module from Github and compile it?

When it comes to Github repositories containing node modules, there are a variety of options available for installation. Some are published as NPM packages and can be easily installed using npm install <module>. Other times, the repository only inclu ...

What steps can you take to intentionally cause errors in a Node Express application in order to capture them in Sentry?

Currently, I am utilizing log4js logger with node express to log errors to a file. However, the log files are quite difficult to interpret and I rarely look at them. I recently integrated Sentry into my project. I want to be able to manually send errors t ...

How can I employ CSS files within a Node module that is compatible with Next?

I recently made the switch from Gatsby to Next and I'm still learning the ropes. When working with Gatsby, I had a Node module that served as my UI library across different projects. This module utilized a CSS module file (style.module.css) that coul ...

What is the best way to manage the parameter in a route?

I set up a few routes : var express = require('express'); var router = express.Router(); router.post('/isAccessible/:orgaId', function(req, res, next) { res.send("------ param = "+orgaId); }); module.exports = router; Next, in a ...

The presence of an unauthorized token within the meteor/node module has been detected, specifically related

While following g00glen00b's tutorial on meteor/twitter integration (), I encountered a persistent error. Any assistance or clues would be greatly appreciated. Steps I've Taken Uninstall/reinstall npm Uninstall/reinstall twitter package Uninst ...

Need a key file from a specific path within a Google Cloud Function

I'm working with Google Cloud Functions that require a JSON key file. Here is an example: const SERVICE_ACCOUNT_EMAIL = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41382e34331e322433372822241e2022222e342f351e242 ...

What is the process for including additional information in a JSON file?

I've been searching for a while now and couldn't find the right solution, so I'm posting my issue here. I am trying to add some data (an object) to a .json file, but each time I add more data, it ends up getting messed up. Here's the co ...

Having trouble with installing npm packages? The node-gyp has encountered an end of line error while scanning the common.gypi

As I try to fetch dependencies for my React app using npm i, I encounter the following error: gyp info spawn args 'build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! Traceback (most recent ...

Issue: make command not located

I'm facing an issue while trying to install packages that require node-gyp. The error message I encounter is as follows: protobuf package resulted in the same outcome.

Despite common suggestions pointing towards a missing make command, I have co ...

Unable to configure node and express for file serving

Currently using Windows 7, I have successfully installed nodes 0.10.12 and the latest versions of express and express-generator globally. I then proceeded to create a new express project called nodetest1 and installed all dependencies using npm install. U ...

Is there a way to transform MongoDB Controller elements into JSON using perl?

require 'MongoDB'; my $info = $db->items->find('something'); my $json_data; while($info->next){ add the information to a $json_data } In search of a solution my @items = $info->all; foreach (@items){ ...

What could be causing my node.js terminal to return duplicate responses for a single request?

// app.js code const express = require("express"); const app = express(); const bodyParser = require("body-parser"); app.set("view engine","ejs"); app.use(bodyParser.urlencoded({extended:true})) app.get("/",function(req,res){ res.render("home"); }); ...

Error message: The specified file or directory does not exist in the npm

Recently, I reinstalled my node from version 12 to version 8. After checking the PATH System environment and setting the npm prefix, everything seemed fine. However, when attempting to use Firebase, an error message saying "No such file or directory" appe ...

What are the steps to developing a chat application with MERN stack or another straightforward method?

What is the best way to develop a chat application for direct communication between two individuals? For instance, imagine a website with numerous producers where a consumer wishes to engage in a live chat with a specific producer. Any ideas on how to ach ...

Encountered a problem while attempting to set up puppeteer within a Docker container

I am currently facing an issue with a Docker container I have set up specifically for Ruby on Rails. This container is built from a Ruby docker container, and after successfully running bundle install and all other tasks, it encounters an error while tryin ...

Update specific fields in a MySQL database using Express.js only if they are passed as parameters

After spending several days trying to set up a REST API, I found a helpful tutorial that explained the basics of sending requests and receiving responses. The only issue is that the tutorial uses MongoDB and Mongoose, while I'm working with MySQL. Due ...

What is the best location to store custom Handlebars helpers within an Express application?

Currently, I am delving into the realm of Node.js and diving into an application with Express + Handlebars. I have reached a part where I must create my own helper for the Handelbars view engine. After defining my helper using the registerHelper() method ...

Navigating through directory paths in JavaScript can be a daunting task for many

In my app.js file, I've included the following code: app.use(multer({dest:'./uploads'})) What does './uploads' refer to here? It is located in the same directory as app.js. In what way does it differ from simply using uploads? I ...