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

标签 arrays json node.js mongodb mongoose

我有一个 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 元素的正确方法?

最佳答案

非常简单。只是让事实成为一个数组。 所以改变

Facts: {                
            type:Array
        },

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

关于arrays - 用于存储子文档的 JSON 对象数组的 Mongoose 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203859/

相关文章:

Java do while循环数组列表问题

javascript - 如何在 Rails 中的 Controller 操作成功或失败的 View 中显示 JSON 状态消息?

C# |从 URL 抓取 JSON 无法将字符串转换为 int

java - 从 void 方法返回数组

javascript - 为什么跟踪代码管理器未检测到我的数据层?

node.js - Nodejs Express Passport - req.isAuthenticated() 和 req.user 之间的区别

node.js - 如何打开需要 Node.js 的 Web 应用程序

javascript - 由于 promise 链,Firebase Firestore 更新查询未执行。我究竟做错了什么?

Javascript从动态名称访问嵌套对象

json - 使用单个表绑定(bind)数组/结构中的多个列