node.js - 如何为具有非结构化数组的 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({}) 。
解决方案
尝试这个:
let ScrapeSchema = new Schema({
title: { type: [String], default: [] },
date: { type: [String], default: [] },
summary: { type:[String], default: [] }
});
或date: { type: [Date] }
。
推荐阅读
- javascript - 如果字符串太大,如何按位或两个二进制字符串(仅包含 0 和 1)?(说字符串长度可达 500)
- javascript - 正则表达式使用 javascript 将数字转换为巴西货币
- c - c代码:fprintf在文件上打印的次数少于预期
- java - MongoDB java驱动程序将未定义作为值传递
- django - 使用旧数据库正确设置 Django 外键
- javascript - 有人请解释一下这里发生了什么
- javascript - JQuery .on("click", functionName) 没有运行
- c++ - 如何通过大量文件和函数在 C++ 中运行对象
- if-statement - Lua Tween 部分信息
- c# - 如何获取和设置列表类属性?