用于存储JSON对象数组的子文档的Mongoose模式

5

我有一个JSON对象,希望使用mongoose创建一个模式(schema)

    { 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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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元素的正确方法吗?
1个回答

22
Facts: {                
            type:Array
        },

Facts: []添加到其中,它将能够存储你在Facts JSON字段中拥有的对象数组。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接