我有一个 JSON 对象,我想创建一个使用 mongoose 的模式
{ ProjectName: 'asdf',
Embargo: 'Yes',
Angle: '1',
Facts: '[{"count":1,"fact":"dsafdsaf","content":"dsafdsaf"}, {"count":3,"fact":"dsafdsaf","content":"dsafdsaf" } , {"count":2,"fact":"dsafdsaf","content":"dsafdsaf"}]',
About: '<p>Anthony Bodin </p>',
EditorNote: 'No',
OrderId: 'tok_14kGRO2Jju1nvjb47YF9jQTJ',
Payment: 'Paid' }
我的问题是 Facts 元素将包含对象数组,我不太确定如何将它保存到数据库中 这是我现在拥有的架构
var ArticleSchema = new mongoose.Schema({
userId: {
type: String,
default: ''
},
userEmail: {
type: String,
default: ''
},
article: {
date: {
type: Date,
default: Date()
},
ProjectName: {
type: String,
default: ''
},
Embargo: {
type: String,
default: true
},
Angle: {
type: String,
default: ''
},
Facts: {
type:Array
},
About: {
type:String,
default:''
},
EditorNote: {
type:String,
default:''
},
Payment: {
type: String,
default: 'Not Paid'
},
OrderId: {
type:String,
default:''
}
}
});
将数据保存为数组是否是保存 Facts 元素的正确方法?
最佳答案
非常简单。只是让事实成为一个数组。 所以改变
Facts: {
type:Array
},
到 Facts: []
,它将能够存储您在 Facts
json 字段中的对象数组。
关于arrays - 用于存储子文档的 JSON 对象数组的 Mongoose 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203859/