bulk.insert(doc) 默认值无法插入 MongoDB nodejs
Posted
技术标签:
??????bulk.insert(doc) ??????? MongoDB nodejs??????:bulk.insert(doc) default value not able to inserted MongoDB nodejs ??????:2021-07-31 15:35:41 ??????:???? 100k ???,????????????,?????bulk.insert(doc)
,???????? mongoose ? Nodejs ???? createdAt
? updatedAt
?????????????????? 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"
??????:
???????????????????????:true? @turivishal ??,??????,????? ??answer ?????????????,????????? createdAt ? updatedAt ??,???????????? @turivishal,??????timestamps: true
??????remarks
???????????
??????? npm mongoose
? mongodb
,?????
?????1?:
??????????request in mongoose github,??????comment??:
initializeUnorderedBulkOp()
?initializeOrderedBulkOp()
api ??,? mongodb “???”???? bulkWrite() API ??,?? MongoDB ?? CRUD ??????,???????????????,? 4.9.0 ??,mongoose ?? Model.bulkWrite() ??,???????????????? ref ?????
?????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')
);
????:
?????bulkWrite
API ??? 5??????????? 15 ???????????batchSize
?????? 5000 ????
????bulkWrite??????batchSize??,?????????????,???????????????????2?:
??
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
????:
createdAt
? updatedAt
????????????,? time
?date
?``` remarks
?????? MongoDB ??
????????null
remarks
????????,????? MongoDB???,???????????,???????,???? MongoDB ??? 6 ???? 3 ????????? 3 ?????????????3?:
?????? Schema ????????,? Mongoose ????:
var dispositionSchema = new Schema(.., timestamps: true );
????:
remarks: null
??? null ???? MongoDB ??以上是关于bulk.insert(doc) 默认值无法插入 MongoDB nodejs的主要内容,如果未能解决你的问题,请参考以下文章
获取 Bulk.Insert() -Mongoskin 的插入 ID
获取 Bulk.Insert() -Mongoskin 的插入 ID