【问题标题】:Mongoose schema for storing an array of JSON objects for subdocuments用于存储子文档的 JSON 对象数组的 Mongoose 模式
【发布时间】:2014-11-29 23:34:49
【问题描述】:

我有一个 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' }

我的问题是 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 元素的正确方法吗?

【问题讨论】:

    标签: arrays json node.js mongodb mongoose


    【解决方案1】:

    它非常简单。只需将事实设为数组即可。 所以改变

    Facts: {                
                type:Array
            },
    

    Facts: [],它将能够将您拥有的对象数组存储在Facts json 字段中。

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 2020-05-15
      • 1970-01-01
      • 2021-01-10
      • 2021-05-03
      • 2012-10-04
      • 2016-01-08
      • 2013-09-20
      • 2018-06-01
      相关资源
      最近更新 更多