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

Baz*_*777 3 arrays json mongoose mongodb node.js

我有一个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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>',
  EditorNote: 'No',
  OrderId: 'tok_14kGRO2Jju1nvjb47YF9jQTJ',
  Payment: 'Paid' }
Run Code Online (Sandbox Code Playgroud)

我的问题是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:''
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

将数据保存为数组是保存Facts元素的正确方法吗?

Jay*_*ram 20

它非常简单.只需将事实作为一个数组.所以改变

Facts: {                
            type:Array
        },
Run Code Online (Sandbox Code Playgroud)

to Facts: []和它将能够在Factsjson字段中存储您拥有的对象数组.