如何用猫鼬生成ObjectId? [英] How can I generate an ObjectId with mongoose?

查看:62
本文介绍了如何用猫鼬生成ObjectId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Mongoose生成一个MongoDB ObjectId.有没有办法从猫鼬访问ObjectId构造函数?

I'd like to generate a MongoDB ObjectId with Mongoose. Is there a way to access the ObjectId constructor from Mongoose?

  • 这个问题是关于从头开始生成新 ObjectId的问题.生成的ID是全新的通用唯一ID.

  • This question is about generating a new ObjectId from scratch. The generated ID is a brand new universally unique ID.

另一个问题是关于使用现有字符串表示形式创建ObjectId 的问题.在这种情况下,您已经具有ID的字符串表示形式(它可能是通用的也可能不是唯一的),并且正在将其解析为ObjectId.

Another question asks about creating an ObjectId from an existing string representation. In this case, you already have a string representation of an ID—it may or may not be universally unique—and you are parsing it into an ObjectId.

推荐答案

您可以在require('mongoose').Types上找到ObjectId构造函数.这是一个示例:

You can find the ObjectId constructor on require('mongoose').Types. Here is an example:

var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();

id是新生成的ObjectId.

您可以在 Mongoose#Types文档Types对象的更多信息. >.

You can read more about the Types object at Mongoose#Types documentation.

这篇关于如何用猫鼬生成ObjectId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆