I am trying to perform bulk upload in node js and mongodb
is my db,can anyone suggest me some best articles regarding this.Thanks in advance.
How to perform bulk upload in mongoose (node.js)
3k views Asked by MMR At
2
There are 2 answers
1
On
You can use Model.collection.insert
or Model.insertMany
as below where collections
is array of items in bulk.
Model.collection.insert(collections, function (err, models) {
next(err, models);
});
OR,
Model.insertMany(collections, function (err, models) {
next(err, models);
});
Mongoose reference: http://mongoosejs.com/docs/api.html#model_Model.insertMany
Mongo reference: https://docs.mongodb.com/v3.2/reference/method/db.collection.insert/
You can insert multiple records with batch/bulk insert in mongoose.
Let's say i have an excel file employees.xlsx with following data and i want perform bulk write.
There are several libraries out there for converting excel data to json in node, i use xlsx but it's personal taste you can use whatever is convenient for you.
Here is a helper i use for reading "/public/employees.xlsx" file.I found the content from here here.
Now the employee model somehow looks like this.
Now let's use the above code so here is my users.js route whenever i type "localhost:3000/users" it write the csv content to database.
Hope this helps!!