CNode

求教,nodejs如何往mongoDB中批量插入数据

问答
Llytangus发布于10 年前最后回复10 年前2 回复6896 浏览0 收藏

本人刚学,求各位给个思路,具体点最好,先谢啦

查看回复

回复 (2)

H
hyj1991#1·10 年前

nodejs的nomgo模块有一个insertMany方法 插入一个数组,数组里面每一个对象字面量就是你要批量插入的数据

Y
yizhiheng#2·10 年前
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)
  })
}
参与回复
登录后即可参与回复。登录