26

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?

ek_ny
  • 10,153
  • 6
  • 47
  • 60

4 Answers4

15

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' } 
}
Harunojikan
  • 547
  • 2
  • 6
  • 15
11

The documentation says: "To specify a type of ObjectId, use Schema.Types.ObjectId in your declaration."... "or just Schema.ObjectId for backwards compatibility with v2".

So I use "Schema.Types.ObjectId".

9

I know this is an old topic but I ran into some trouble in this area recently, so here's what I found in case anyone else's banging their heads against the wall:

Whenever you use a reference in your schema to an ObjectId, you MUST use mongoose.Schema.Types.ObjectId.

mongoose.Types.ObjectId should be used solely for its constructor when building new objectIds from a string hex or, as it's probably the most common use case, to validate a string as an ObjectId hex, say:

try {
  let oid = new mongoose.Types.ObjectId('XXXXXXXXX')
} catch (e) {
  console.error('not a valid ObjectId hex string ==> ', e)
}
Felipe Vignon
  • 451
  • 3
  • 9
2

There isn't much of a difference, but they are available for separate uses.

Schemas should always use mongoose.Schema.Types.

mongoose.Types are the object you work with within a mongoose document. These are special array subclasses, buffer subclasses, etc that we've hooked into or created special to ease updates and track changes.

Source: https://github.com/Automattic/mongoose/issues/1671

Pang
  • 9,564
  • 146
  • 81
  • 122
rqmok
  • 834
  • 2
  • 9
  • 20