本文介绍了猫鼬中的对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在定义猫鼬模式,定义如下:
I am defining a mongoose schema and definition is as follows:
inventoryDetails: {
type: Object,
required: true
},
isActive:{
type:Boolean,
default:false
}
我尝试使用对象"类型,并且看到数据已成功保存.当我将类型更改为数组时,保存失败.
I tried "Object" type and I am seeing my data is getting saved successfully. When I changed type to array, the save is failing.
样本数据:
{
"inventoryDetails" : {
"config" : {
"count" : {
"static" : { "value" : "123" },
"dataSource" : "STATIC"
},
"title" : {
"static" : { "value" : "tik" },
"dataSource" : "STATIC"
}
},
"type" : "s-card-with-title-count"
}
}
对象"类型不是猫鼬允许的类型之一.但是,它如何得到支持?
"Object" type is not one of the types that mongoose allows. But, how it is being supported ?
推荐答案
您有两个选择可以将Object
放入数据库:
You have two options to get your Object
in the db:
let YourSchema = new Schema({
inventoryDetails: {
config: {
count: {
static: {
value: {
type: Number,
default: 0
},
dataSource: {
type: String
}
}
}
},
myType: {
type: String
}
},
title: {
static: {
value: {
type: Number,
default: 0
},
dataSource: {
type: String
}
}
}
})
看看我的真实代码:
let UserSchema = new Schema({
//...
statuses: {
online: {
type: Boolean,
default: true
},
verified: {
type: Boolean,
default: false
},
banned: {
type: Boolean,
default: false
}
},
//...
})
此选项使您能够定义对象的数据结构.
This option gives you the ability to define the object's data structure.
如果要使用灵活的对象数据结构,请参阅下一个.
If you want a flexible object data structure, see the next one.
示例取自文档:
let YourSchema = new Schema({
inventoryDetails: Schema.Types.Mixed
})
let yourSchema = new YourSchema;
yourSchema.inventoryDetails = { any: { thing: 'you want' } }
yourSchema.save()
这篇关于猫鼬中的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!