首页 > 解决方案 > 有没有办法使用循环随着时间的推移将多个文档发送到 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

标签: javascriptnode.jsmongodbstreamreal-time

解决方案


我找到了一个解决方案,使用 Cron Job 。你可以通过安装它 npm i cron


推荐阅读