定义猫鼬模式时应使用Schema.Types.ObjectId还是Schema.ObjectId [英] Should I Use Schema.Types.ObjectId or Schema.ObjectId When Defining a Mongoose Schema

查看:610
本文介绍了定义猫鼬模式时应使用Schema.Types.ObjectId还是Schema.ObjectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像是这样定义我的架构的:

It seems like defining my Schema this way:

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

或这种方式:

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

两个工作.我看到猫鼬指南使用Schema.Types.ObjectId

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?

推荐答案

没关系.两者是完全一样的.如果您实际上是console.log(mongoose.Schema);,则可以看到mongoose.Schema.ObjectIdmongoose.Schema.Types.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' } 
}

这篇关于定义猫鼬模式时应使用Schema.Types.ObjectId还是Schema.ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆