javascript - 如何将 NeDB 对象存储在集合中?
问题描述
我想使用 NeDB 作为一个非常简单的 ExpressJS 应用程序的数据库。我想将对象集合存储到一个单独的文件中,所以一个文件用于订单(orders.json)。但是,每当我将订单对象插入数据存储区时,它只是作为一个额外的对象附加,而不是创建一个数组。
var Datastore = require('nedb')
, orders = new Datastore({ filename: __dirname + '/orders.json', autoload: true });
const order = {
chatId: 4324234,
url: 'https://google.com',
maxPrice: 200,
incrementPrice: 2,
expiryHours: 24,
timestamp: Date.now()
}
orders.insert(order);
orders.insert(order);
这将对象存储为:
{"chatId":1,"url":"https://google.com","maxPrice":200,"incrementPrice":2,"expiryHours":24,"timestamp":1631185683197,"_id":"mh9tdb06Tw29JXSW"}
{"chatId":1,"url":"https://google.com","maxPrice":200,"incrementPrice":2,"expiryHours":24,"timestamp":1631185691156,"_id":"8Dg6GXFlygYMPmRZ"}
正如您可能已经看到的,这是无效的 JSON。我希望它们存储在一个数组中(一个包含所有订单的数组)。
如何使用 NeDB 实现这一目标?
解决方案
推荐阅读
- ruby-on-rails - Rails db 迁移错误
- python - 在 macOS 上通过 Python 打开 Excel
- msbuild - 框架与 Visual Studio 中的 Msbuild 版本
- python - 如何将字符串数组分成 2 个单独的部分
- java - Java SWT 浏览器。具有超高清 (4K) 或更高分辨率的屏幕上的不同输出
- python - flask + restful web service 无法运行文本分类模型 + python
- gem5 - 如何在完整的系统 gem5 模拟中获得图形 GUI 输出和用户触摸/键盘/鼠标输入?
- git - 如何在 VSTS 中使用外部 powershell 模块
- database - 在 Laravel Voyager 中创建表时创建迁移文件
- java - 带有 maven intel IDEA 的包不存在 javaFX