我有一个来自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
上测试了它的效果,但对于提供每个Ticket
的LogTicket
,我只有ticket_no
作为这两个a的链接器。
如何使用mongoDb通过ticket_no
批量导入LogTicket
,以便填充每个Ticket
集合?
发布于 2017-09-22 06:29:07
使用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);
});
https://stackoverflow.com/questions/46356803
复制