批量导入基于相同属性的mongodb集合

时间:2017-09-22 04:19:38

标签: mongodb mongoose mongoose-schema mongoose-populate nosql

我有一个来自mySql的旧数据库需要导入到mongoDb,我已经创建了两个集合的新mongoDb模式,如下所示,

let Ticket = new Schema({
  ticket_no: String,
  title: String,
  logTickets: [{type: Schema.Types.ObjectId, ref: 'LogTicket'}]
});

let LogTicket = new Schema({
  user: String,
  timestamp: Date,
  log: String,
  ticket: {type: Schema.Types.ObjectId, ref: 'Ticket'}
});

我已经处理了转换为json并测试它在Ticket上工作,但是LogTicket提供了每个Ticket,我只有ticket_no作为这些的链接器2。

如何使用mongoDb按LogTicket批量导入ticket_no,以便填充每个Ticket集合?

1 个答案:

答案 0 :(得分:0)

使用mongoose的populate函数

Ticket.
  findOne({ _id: refObjectId }).
  populate('logTickets').
  exec(function (err, ticket) {
    if (err) return throw err;
    console.log('The LogTickets are %s', ticket.logTickets);
  });

进一步阅读http://mongoosejs.com/docs/populate.html