I have inserted 100k records, I have uploaded records batch-wise successfully. But, If I use the bulk.insert(doc)
the default value not inserted using mongoose and Nodejs. like createdAt
and updatedAt
field as default value not inserted. I trying to add the options setDefaultsOnInsert: true
bulk.insert
no option to add the value. Currently, I added my code. Please help me out for advance.
code
let data = req.body;
var bulk = callDispositionModel.collection.initializeOrderedBulkOp();
var counter=0
data.forEach(doc1 => {
bulk.insert(doc1);
if (counter % 5000 == 0) {
bulk.execute();
bulk = callDispositionModel.collection.initializeUnorderedBulkOp();
counter = 0;
}
counter++
})
if (counter > 0) {
bulk.execute(function(err,result) {
if(err){
console.log(`err `, err)
}else{
console.log(`result `, result)
return res.send({success: true, message:'data uploaded successfully')
}
});
}
Schema or Model
let dispositionSchema = new mongoose.Schema({
name : {type: String, default: null},
mobile : {type : String, default: null},
remarks : {type: String, default:null},
duration: {type : String, default: null},
amount : {type : Number, default: 0},
date : {type : String, default: null},
time : {type : String, default: null},
createdAt: {type: Date, default: Date.now },
updatedAt: {type: Date, default: Date.now }
});
const disposition = mongoose.model('disposition', dispositionSchema);
export default disposition;
Data
It's inserted the data in mongodb
{
"_id" : ObjectId("6098e6d007e2804b9c1f8317"),
"name" : "senthil",
"amount" : 0
}
{
"_id" : ObjectId("6098e6d007e2804b9c1f8316"),
"name" : "periyas",
"amount" : 0
}
But, I have expected the output
Expected Data
{
"_id" : ObjectId("6098e6d007e2804b9c1f8317"),
"name" : "senthil",
"amount" : 0,
"mobile" : null,
"remarks" : null,
"createdAt": "2021-05-07T13:55:34.233Z"
},
{
"_id" : ObjectId("6098e6d007e2804b9c1f8316"),
"name" : "periyas",
"mobile" : null,
"remarks" : null,
"createdAt": "2021-05-07T13:55:34.233Z"
}