简体   繁体   English

如何在 Mongo Stitch 函数中生成 Mongo ObjectId?

[英]How can I generate a Mongo ObjectId in a Mongo Stitch Function?

I am trying to generate a Mongo ObjectID in a stitch function before object insertion, and keep getting errors.我试图在对象插入之前在缝合函数中生成一个 Mongo ObjectID,并不断出错。 I've tried const _id = ObjectId();我试过const _id = ObjectId(); and also const _id = mongoService.ObjectId() and keep getting errors.还有const _id = mongoService.ObjectId()并不断出错。 Is it possible to generate a Mongo ObjectId in a stitch function before inserting an item in collection?在集合中插入项目之前,是否可以在缝合函数中生成 Mongo ObjectId?

Is it possible to generate a Mongo ObjectId in a stitch function before inserting an item in collection?在集合中插入项目之前,是否可以在缝合函数中生成 Mongo ObjectId?

Yes, to generate a new ObjectId you can use the BSON utility package :是的,要生成新的 ObjectId,您可以使用BSON 实用程序包

let id = new BSON.ObjectId(); 

You can also turn string hex to ObjectId using similar call, ie:您还可以使用类似的调用将字符串十六进制转换为 ObjectId,即:

let id = new BSON.ObjectId("5e5dd3547e96ac1963d1b841"); 

See also Stitch: Utility Packages for a list of utility packages provided.另请参阅Stitch:实用程序包以获取提供的实用程序包列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM