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?
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.
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.
Mongoose does not natively support long and double datatypes for example, although MongoDB does.
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.
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' } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With