Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

mongodb - What is the difference between mongoose.ObjectId and mongoose.Types.ObjectId?

It's mentioned in the docs the following:

An ObjectId is a special type typically used for unique identifiers...

const carSchema = new mongoose.Schema({ driver: mongoose.ObjectId });

ObjectId is a class, and ObjectIds are objects....etc

const Car = mongoose.model('Car', carSchema);

const car = new Car();
car.driver = new mongoose.Types.ObjectId();

typeof car.driver; // 'object'
car.driver instanceof mongoose.Types.ObjectId; // true

car.driver.toString(); // Something like "5e1a0651741b255ddda996c4"

What is meant by ObjectId (the bold text above in the quote)? is it mongoose.ObjectId or mongoose.Types.ObjectId?

And what's the difference between them?

The URL of the docs:

https://mongoosejs.com/docs/schematypes.html#usage-notes

at the ObjectIds section

question from:https://stackoverflow.com/questions/65831345/what-is-the-difference-between-mongoose-objectid-and-mongoose-types-objectid

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to schema types docs

A SchemaType is different from a type. In other words, mongoose.ObjectId !== mongoose.Types.ObjectId. A SchemaType is just a configuration object for Mongoose. An instance of the mongoose.ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.

Also into docs:

You can think of a Mongoose schema as the configuration object for a Mongoose model. A SchemaType is then a configuration object for an individual property. A SchemaType says what type a given path should have, whether it has any getters/setters, and what values are valid for that path.

So, mongoose.Types.ObjectId is a "mongoose object" while mongoose.ObjectId is a "mongodb object".

Edit to answer the comments.

To the first question, for me is: Yes. As a developer that use mongoose I only use mongoose functions. That implies use mongoose.Types.ObjectId instead of mongoose.ObjectId.

Also if you try to do mongoose.ObjectId() you will get a warning 'To create a new ObjectId please try Mongoose.Types.ObjectId instead of using Mongoose.Schema.ObjectId'

About the second question, if I've understood correctly, yes too. You can check this.

Create a new schema and do:

const MongooseModel = new mongoose.Schema({_id: mongoose.Types.ObjectId})

MongooseModel.path('_id') instanceof mongoose.Schema.ObjectId // true
MongooseModel.path('_id') instanceof mongoose.SchemaTypes.ObjectId // true
MongooseModel.path('_id') instanceof mongoose.Schema.Types.ObjectId // true
MongooseModel.path('_id') instanceof mongoose.ObjectId // true
MongooseModel.path('_id').instance // ObjectID

MongooseModel.path('_id') instanceof mongoose.Types.ObjectId // false
mongoose.Schema.ObjectId instanceof mongoose.Types.ObjectId // false

Your schema _id is created via mongoose.Types and is instance of mongoose.ObjectId. Also is not an instance of the type, as explained into docs "A SchemaType is different from a type".

The _id generated is an instance of the ObjectID but is not an instance of the "parent" who generates the value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...