Javascript 如何在ETL中处理无效的Mongo/Mongoose ObjectId分配

Javascript 如何在ETL中处理无效的Mongo/Mongoose ObjectId分配,javascript,mongodb,mongoose,sybase,Javascript,Mongodb,Mongoose,Sybase,我创建了一个ETL来将数据从sybase传输到mongoDB环境。在一个地方,为了在模型中填充正确的mongo ObjectId,我正在与遗留id进行匹配 这适用于我的前几千条记录,直到我遇到一条缺少遗留id的记录,在这种情况下,我会得到一个错误,因为模型需要一个objectId。在我的ETL文件中执行空检查不足以处理此问题。我还需要处理这样一个事实,即模型需要一个有效的objectId。我该怎么做 我的相关ETL代码如下所示: let agency = await Agency.findOne

我创建了一个ETL来将数据从sybase传输到mongoDB环境。在一个地方,为了在模型中填充正确的mongo ObjectId,我正在与遗留id进行匹配

这适用于我的前几千条记录,直到我遇到一条缺少遗留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
 }
我是这样来的:

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
 }
然后数据被移植到模型中,如下所示:

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
 }