求教,nodejs如何往mongoDB中批量插入数据
本人刚学,求各位给个思路,具体点最好,先谢啦
2 回复
nodejs的nomgo模块有一个insertMany方法 插入一个数组,数组里面每一个对象字面量就是你要批量插入的数据
MongoInit.prototype.initTasks = function (db, ids, done) {
db.collection('tasks', function (err, tasks) {
if (err) {
return done(err)
}
tasks.insertMany([
{
ownerId: '53a32091f663e776169bdb4a',
status: 'PENDING',
type: 'RELOAD',
active: true,
params: { appId: '123' },
schedule: {
startDate: new Date('2025-09-22T19:35:37.482Z'),
endDate: new Date('2025-10-22T19:35:37.482Z'),
frequency: 'PT1H'
}
},
{
ownerId: '53a32091f663e776169bdb4a',
status: 'FAILED',
type: 'RELOAD',
active: false,
params: { appId: '234' },
schedule: {
startDate: new Date('2050-09-22T19:35:37.482Z'),
endDate: new Date('2050-10-22T19:35:37.482Z'),
frequency: 'PT10H'
}
}
], done)
})
}