bulk.insert(doc) 默认值无法插入 MongoDB nodejs

bulk.insert(doc) default value not able to inserted MongoDB nodejs

我插入了100k条记录,我已经成功批量上传记录。但是,如果我使用 bulk.insert(doc) 默认值,则不会使用 mongoose 和 Nodejs 插入。像 createdAtupdatedAt 字段作为默认值未插入。我试图添加选项 setDefaultsOnInsert: true bulk.insert 没有添加选项的值。目前,我添加了我的代码。请帮我预支。

代码

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')
        }
        });
    }

架构或模型

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;

数据

在mongodb

中插入了数据
{
    "_id" : ObjectId("6098e6d007e2804b9c1f8317"),
   "name" : "senthil",
    "amount" : 0
}
{
    "_id" : ObjectId("6098e6d007e2804b9c1f8316"),
    "name" : "periyas",
    "amount" : 0
}
  

但是,我预料到了输出

预期数据

{
    "_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"
}

改变

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 }
});

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 }
}, { timestamps: { createdAt: 'createdAt' , updatedAt: 'updatedAt'} });

More detail here

您现在可以在架构上设置时间戳选项,让 Mongoose 为您处理:

var dispositionSchema = new Schema({..}, { timestamps: true });

管理员支持批量插入 request in mongoose github, and read one of the comment

The initializeUnorderedBulkOp() and initializeOrderedBulkOp() api methods, which is "soft deprecated" by mongodb. It has been replaced by the bulkWrite() API, which is part of MongoDB's core CRUD spec and thus is much more widely implemented. In particular, mongoose has a Model.bulkWrite() function as of 4.9.0 that has validation, casting, promises, and ref depopulating.

您可以使用 bulkWrite() 类似的东西:

let data = req.body;
let bulk = [];    
data.forEach((doc1) => {
    bulk.push({ "insertOne": { "document": doc1 } });
    if (bulk.length === 5000) {
        callDispositionModel.bulkWrite(bulk).then((result) => {});
        bulk = [];
    }
})
if (bulk.length > 0) {
    callDispositionModel.bulkWrite(bulk).then((res) => {
        console.log(res.insertedCount);
        return res.send({success: true, message: 'all data uploaded successfully'})
    });
}