javascript - 有没有办法使用循环随着时间的推移将多个文档发送到 MongoDB?
问题描述
我正在努力使用 javascript 将传感器数据发送到 mongodb 。我找到了一些解决方案,如 mongodb Atlas 等 ..,但我正在寻找一种简单的方法来做到这一点,这是我的代码
const db =client.db(databaseName)
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
while(true) {
const data = async (timestep, temperature) => {
const post = await db.collection('temperature').insertOne({
timestep: timestep,
data: temperature
})
await timeout(3000);
return post
}
data("2020", 23).then((result) => console.log(result))
}
我得到一些错误:
<--- Last few GCs --->
[3454:0x10417e000] 28749 ms: Mark-sweep 2028.1 (2066.2) -> 2027.4 (2065.2) MB, 2078.6 / 0.0 ms (average mu = 0.102, current mu = 0.006) allocation failure GC in old space requested
[3454:0x10417e000] 30843 ms: Mark-sweep 2028.4 (2065.2) -> 2028.0 (2065.7) MB, 2081.6 / 0.0 ms (average mu = 0.055, current mu = 0.006) allocation failure scavenge might not succeed
解决方案
我找到了一个解决方案,使用 Cron Job 。你可以通过安装它
npm i cron
推荐阅读
- node.js - 使用 dgram 发送消息的固定端口
- mysql - ADODB VBA mysql 查询要么给出 E_FAIL 要么截断数据
- javascript - 在滚动时更改活动类
- javascript - Vue PWA - manifest.json 在页面重新加载时被覆盖
- c# - 如何从 contactId 中检索 CRM 动态帐户记录
- flutter - 颤振抽屉背景图片
- amazon-web-services - 如何使用 Hashicorp Packer 创建在启动时运行 docker 映像的 EC2 映像?
- php - Mockery_0_wpdb 的意外错误使用通知
- php - 与 whereHas 的 ManyToMany Eloquent Laravel 查询问题
- python - 我可以确定一个函数在输入时会引发什么错误吗?