如何处理 ETL 中无效的 Mongo/Mongoose ObjectId 分配
How to Handle Invalid Mongo/Mongoose ObjectId Assignment in ETL
我创建了一个 ETL 来将数据从 sybase 传输到 mongoDB 环境。在一个地方,我正在匹配遗留 ID,以便在模型中填充正确的 mongo ObjectId。
这适用于我的前几千条记录,直到我 运行 进入缺少遗留 ID 的记录,在这种情况下,我收到一个错误,因为模型需要一个 objectId。在我的 ETL 文件中执行空检查不足以处理此问题。我还需要处理模型需要有效 objectId 的事实。我该怎么做?
我的相关 ETL 代码如下所示:
let agency = await Agency.findOne({ agencyId: record.agent_house }).exec();
我是这样插入的:
agency: {
id: agency._id ? agency._id : null,
// Other prop,
// Other prop
}
然后数据被移植到模型中,如下所示:
agency: {
id: { type: mongoose.Schema.Types.ObjectId, ref: 'agencies' },
// other prop,
// other prop
}
如何处理没有值的情况,即使在 ETL 文件中进行了空检查,也会导致 objectId 分配失败?
使用 mongoose ObjectId 类型,您可以:
agency: {
id: agency._id ? agency._id : new mongoose.mongo.ObjectId(),
// Other prop,
// Other prop
}
我创建了一个 ETL 来将数据从 sybase 传输到 mongoDB 环境。在一个地方,我正在匹配遗留 ID,以便在模型中填充正确的 mongo ObjectId。
这适用于我的前几千条记录,直到我 运行 进入缺少遗留 ID 的记录,在这种情况下,我收到一个错误,因为模型需要一个 objectId。在我的 ETL 文件中执行空检查不足以处理此问题。我还需要处理模型需要有效 objectId 的事实。我该怎么做?
我的相关 ETL 代码如下所示:
let agency = await Agency.findOne({ agencyId: record.agent_house }).exec();
我是这样插入的:
agency: {
id: agency._id ? agency._id : null,
// Other prop,
// Other prop
}
然后数据被移植到模型中,如下所示:
agency: {
id: { type: mongoose.Schema.Types.ObjectId, ref: 'agencies' },
// other prop,
// other prop
}
如何处理没有值的情况,即使在 ETL 文件中进行了空检查,也会导致 objectId 分配失败?
使用 mongoose ObjectId 类型,您可以:
agency: {
id: agency._id ? agency._id : new mongoose.mongo.ObjectId(),
// Other prop,
// Other prop
}