Ratan Uday Kumar
Ratan Uday Kumar

Reputation: 6482

how to insert 10K records in mongodb consisting of Same keys and values

I am working on server side development for the game application.

The game will have a spin wheel concept. For that purpose, I need to store 10k records for 100 coins and 5k records for 200 coins and 100 records for 500 coins and 1 record for 1000 coins as well as for 10000 coins.

I will store documents by running a cron job at the end of the day. And will remove all records of the previous day.

Every time the spin wheel will be called I will pick the random document from Available(Non utilized) document.

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

I can use for loop and can insert 10k records one after another.

But I need a speedy and efficient approach for the above Algorithm.

Upvotes: 2

Views: 3529

Answers (2)

Nikhil Unni
Nikhil Unni

Reputation: 779

const createArray = (arr,times,obj) => arr.flatMap((x,i) => Array(obj[i] || times).fill(x))
var arr = [{
"No_of_Coins": 100,
"position": 4,
"Recorded_Spin": false,
"date": new Date()
}]
var arr2 = createArray(arr,1000,arr[0])
console.log(arr2)

Lets try this one.

Upvotes: 1

hoangdv
hoangdv

Reputation: 16127

Just create a array what includes 10000 items, use insertMany method to insert all item to your db

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

db.collection.insertMany(arrData);

Upvotes: 5

Related Questions