Meteor Mongo BulkOp将ObjectID转换为普通对象

在使用Meteor时,我有时会访问底层的Node Mongo驱动程序,所以我可以进行批量更新和插入。

const bulk = Coll.rawCollection().initializeOrderedBulkOp(); bulk.insert({key_id: Mongo.Collection.ObjectID()}); // note key_id is an ObjectID ... bulk.execute(); 

但是,当我在插入后查看数据库时, key_id字段的值最终成为普通的子文档{_str: '...'}

有什么方法可以在Node的Mongo库中使用批量操作(不pipe它是Meteor使用的),还是将ObjectID保留为Mongo的ObjectIDtypes?

(关于不同IDtypes的性质有很多post,并解释了Minimongo等。我对将ObjectID转换为普通对象并解决该问题的批量操作特别感兴趣。

来自尼尔的顶级评论

在本地方法你实际上需要抓住本地的实现。 您应该能够通过MongoInternals […]从加载的驱动程序访问

Mongo.Collection.ObjectID不是普通的ObjectId表示,实际上是Meteor内部使用的复杂对象。 因此,为什么本地方法不知道如何使用这个值。

所以如果你有一个ObjectId字段,并且你正在使用Meteor Collection的rawCollection一些方法(例如,

  • .distinct
  • .aggregate
  • .initializeOrderedBulkOp
  • .initializeUnorderedBulkOp

),你会想要转换你的ObjectId的使用

 const convertedID = new MongoInternals.NpmModule.ObjectID( originalID._str ); // then use in one of the arguments to your function or something const query = {_id: convertedID}; 

在调用它们的方法之前。