gpt4 book ai didi

mongoose - 定义 Mongoose 架构时应该使用 Schema.Types.ObjectId 还是 Schema.ObjectId

转载 作者:行者123 更新时间:2023-12-02 16:26:40 25 4
gpt4 key购买 nike

这似乎是这样定义我的架构:

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"}
});

两者都有效。我看到 Mongoose 指南使用 Schema.Types.ObjectId

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

但我很困惑两者都有效。

架构应该使用哪一个?两者有什么区别?

最佳答案

没关系。两者完全相同。如果你实际上 console.log(mongoose.Schema); 你可以看到 mongoose.Schema.ObjectIdmongoose.Schema.Types.ObjectId > 指的是完全相同的事物。

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

关于mongoose - 定义 Mongoose 架构时应该使用 Schema.Types.ObjectId 还是 Schema.ObjectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28997636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com