Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Use Schema.Types.ObjectId or Schema.ObjectId When Defining a Mongoose Schema

Tags:

It seems like defining my Schema this way:

var PossessionSchema = new mongoose.Schema({   thing: {type: mongoose.Schema.Types.ObjectId, ref:"Thing"} }); 

or this way:

var PossessionSchema = new mongoose.Schema({   thing: {type: mongoose.Schema.ObjectId, ref:"Thing"} }); 

Both work. I see that the mongoose guide uses Schema.Types.ObjectId

http://mongoosejs.com/docs/schematypes.html

But I'm confused that both work.

Which one should be used for the Schema? And what is the difference between the two?

like image 870
ek_ny Avatar asked Mar 11 '15 21:03

ek_ny


People also ask

Which are the valid schema types useful while defining a schema in Mongoose?

You can also define MongoDB indexes using schema type options. index : boolean, whether to define an index on this property. unique : boolean, whether to define a unique index on this property. sparse : boolean, whether to define a sparse index on this property.

What does schema types ObjectId mean?

Schema. Types. ObjectId and that's means, it looks like a string of an ID when you get it back from the database but it's not, it's actually an object that converts it to a string.

Which schema types are not supported by Mongoose?

Mongoose does not natively support long and double datatypes for example, although MongoDB does.

What is ObjectId in Mongoose?

An ObjectID is a 12-byte Field Of BSON type. The first 4 bytes representing the Unix Timestamp of the document. The next 3 bytes are the machine Id on which the MongoDB server is running. The next 2 bytes are of process id. The last Field is 3 bytes used for increment the objectid.


1 Answers

It doesn't matter. Both is exactly the same. If you actually console.log(mongoose.Schema); you can see that both mongoose.Schema.ObjectId and mongoose.Schema.Types.ObjectId refer to the exact same thing.

{ [Function: Schema]   reserved: {      _posts: 1,     _pres: 1,     validate: 1,     toObject: 1,     set: 1,     schema: 1,     save: 1,     modelName: 1,     get: 1,     isNew: 1,     isModified: 1,     init: 1,     errors: 1,     db: 1,     collection: 1,     once: 1,     on: 1,     emit: 1    },   interpretAsType: [Function],   Types: {      String: { [Function: SchemaString] schemaName: 'String' },     Number: { [Function: SchemaNumber] schemaName: 'Number' },     Boolean: { [Function: SchemaBoolean] schemaName: 'Boolean', '$conditionalHandlers': [Object] },     DocumentArray: { [Function: DocumentArray] schemaName: 'DocumentArray' },      Embedded: [Function: Embedded],     Array: { [Function: SchemaArray] schemaName: 'Array' },     Buffer: { [Function: SchemaBuffer] schemaName: 'Buffer' },     Date: { [Function: SchemaDate] schemaName: 'Date' },     ObjectId: { [Function: ObjectId] schemaName: 'ObjectId' },     Mixed: { [Function: Mixed] schemaName: 'Mixed' },     Oid: { [Function: ObjectId] schemaName: 'ObjectId' },     Object: { [Function: Mixed] schemaName: 'Mixed' },     Bool: { [Function: SchemaBoolean] schemaName: 'Boolean', '$conditionalHandlers': [Object] }    },   ObjectId: { [Function: ObjectId] schemaName: 'ObjectId' }  } 
like image 98
Harunojikan Avatar answered Sep 18 '22 12:09

Harunojikan