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

前端 未结 4 743
温柔的废话
温柔的废话 2021-02-12 23:39

It seems like defining my Schema this way:

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


        
4条回答
  •  走了就别回头了
    2021-02-13 00:19

    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' } 
    }
    

提交回复
热议问题