首页 > 解决方案 > 如何为具有非结构化数组的 Mongodb 文档定义模式?

问题描述

我正在尝试创建一个 nodejs 站点来显示我已抓取并存储在 mongodb 集合中的数据内容。我可以进行抓取并将数据放入数据库,但我无法将其取出。我猜问题出在我的节点模型中没有正确定义模式。

数据是所有字符串数组。

{
    "_id": {
        "$oid": "5ca553dc97c32c1f05aba38d"
    },
    "title": [
        "Official Foreign Reserves(March 2019)"
    ],
    "date": [
        "2019.04.03"
    ],
   "summary": [
        "The Consumer Price Index was 104.49(2015=100) in March 2019. The index decreased 0.2 percent from the preceding",
        "month and rose 0.4 percent from the same month of the previous year.",
        "The index excluding food and energy was 105.32 in March 2019. The index decreased 0.1 percent from the preceding",
        "month and rose 0.8 percent from the same month of the previous year.",
        "For more information, refer to the attached file.",
        ""
    ]
}

我试图像这样定义架构:

const ScrapeSchema = new Schema({
  title:{
    type: Array, "default" : []
  },
  date:{
    type: Array, "default" : []
  },
  summary:{
    type: Array, "default" : []
  }
});

我回来的对象是空的。Mongodb 连接正常,没有记录错误。

我目前只是在请求中使用 find({}) 。

标签: node.jsmongodbmongoose-schema

解决方案


尝试这个:

let ScrapeSchema = new Schema({      
    title: { type: [String], default: [] },
    date: { type: [String], default: [] },
    summary: { type:[String], default: [] }
});

date: { type: [Date] }


推荐阅读