简体   繁体   中英

mongoose bulk insert in real time

This is the detailed case: A user click on button " Create Codes " which will be generating 50000 codes randomly and save them in a collection "codes" in the format similar to this

[{
    _id : "_id"
    code : "abc",
    orderId : "id",
    status : "status"
},
{
    _id : "_id"
    code : "def",
    orderId : "id",
    status : "status"
}]

I am using bulk.insert() -> bulk.execute() from bulk API and it is taking 1.30 - 2 minutes. Keeping user experience in mind how it can be done more quickly and also user will be seeing or downloading these codes right after completion of this process. I am open for suggestions if anything else can be done by changing the process flow to make it fast for the user.

Thanks in advance.

If your random logic is not complicated, you can generate all 50000 records inside mongodb. This is an instruction on how to put js scripts inside mongo and run it there https://www.mongodb.com/blog/post/boosting-javascript-from-mongo-shell-to-nodejs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM