2014-10-05 66 views
3

我有一個JSON對象,我想創建一個模式用於使用貓鼬對象用於存儲JSON陣列貓鼬架構的子文檔

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

我的問題是,事實元素將包含與對象數組和我不太清楚如何將其保存到數據庫中 這裏是我現在所擁有的

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:'' 
     } 
    } 
}); 

是保存數據作爲數組保存事實元素的正確方法的模式?

回答

16

它非常簡單。只要把事實做成一個數組。 因此改變

Facts: {     
      type:Array 
     }, 

Facts: [],這將是能夠存儲你有沒有在Facts JSON領域對象的數組。

+0

謝謝,這工作。 – Bazinga777 2014-10-05 17:46:10

+0

我很高興幫助。那麼你應該也贊成它。 – 2014-10-05 17:48:51