javascript - 如何在 mongoose 中保存 json 数据

标签 javascript json node.js mongodb mongoose

这是我的 json 数据,我想将其保存在 mongoose 数据库中,但我很困惑如何设计我的 mongoose 架构。

data is { 'posts[0][commentId]': '0',
  'posts[0][id]': '1',
  'posts[0][postName]': 'hi',
  'posts[0][img]': '',
  'posts[0][postBy]': ' Neeraj',
  'posts[1][commentId]': '0',
  'posts[1][id]': '2',
  'posts[1][postName]': 'hii',
  'posts[1][img]': '',
  'posts[1][postBy]': ' Neeraj',
  'posts[1][comments][0][commentId]': '1',
  'posts[1][comments][0][commentValue]': 'hlo how are u???',
  'posts[1][comments][0][commentBy]': ' Neeraj',
  'posts[1][comments][0][upCounter]': '5',
  'posts[1][comments][0][downCounter]': '6' }

这是我的帖子架构,但我不知道如何实际设计它。

var postSchema=new Schema({
    posts:[{
         commentId:  Number ,
  
    comments : [{
      commentBy : String,
        commentId  : String,
        commentValue:String,
        date:Date,
        downCounter:Number,
        upCounter:Number
    }],
id:String,
img:String,
postBy:String,
postDate:Date,
postName:String
}]
});

最佳答案

您只需将 posts 属性设置为 mongoose 架构中的对象并存储整个对象即可。

像这样的事情:

var postSchema = new Schema({
      posts: {
        type: Object,
        required: false //depends on whether the field is mandatory or not
      }
    });

关于javascript - 如何在 mongoose 中保存 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47700472/

相关文章:

php - 是否有适用于 PHP 或 JavaScript 的良好 JSON 格式化程序库?

Objective-C JSON bool 值

node.js - Lambda Axios 发布到第二个 API 奇怪的异步行为

javascript - 关于设置 something.prototype.__proto__ 的困惑

javascript - 使用 ng-click 调用链接到我的项目的 JavaScript 函数

javascript - 查找两个数组的最长公共(public)后缀的最佳方法

node.js - Nodejs 部署到 Heroku 时出错

javascript - vue js中登录成功后如何重定向到dashboard组件页面(登录组件)?

java - Jackson MappingException 与 Websphere Application Server

javascript - 错误 : Route. get() 需要回调函数,但得到了一个 [object Undefined] NODE.JS + SQL