【问题标题】:JS output directly to database?JS直接输出到数据库?
【发布时间】:2016-03-19 04:47:01
【问题描述】:

须藤代码:

function findAllPaths(allStarts){
    function makePath(start){
        var path = [{"1,1" : 1}, {"1,2" : 2}, {"2,2" : 3},{"2,1" : 4}]
        return path;
    }
    var solutions = [];
    for(i=0;i<allStarts.length;i++){
         solutions.push(makePath(allStarts[i]));
    }
    return solutions;
}
console.log(findAllPaths(1,1));

// Every time the code runs, it outputs thousands to millions of paths.

有没有办法直接在mongo中运行js脚本?

我的 JS 文件生成一个带有结果的 JSON 对象。我尝试在节点中运行脚本,将输出保存到 .txt 文件中。最终它以错误终止:

100501 毫秒:清除 1413.8 (1457.9) -> 1413.8 (1457.9) MB,30.1 / 0 毫秒(自上次 GC 后 1 步增加 3.4 毫秒)[分配失败] [增量标记延迟标记扫描]。 102369 毫秒:标记扫描 1413.8 (1457.9) -> 1413.8 (1457.9) MB, 1867.8 / 0 ms (+ 4.1 ms in 2 steps in 2 steps since the start of marking, max step 3.4 ms) [最后的 gc]。
104270 毫秒:标记扫描 1413.8 (1457.9) -> 1413.8 (1457.9) MB,1901.7 / 0 ms [最后的 gc]。

==== JS 堆栈跟踪 ========================================== =

安全上下文:0x1b9a709b4629 1: setPrototypeOf [native v8natives.js:~653] [pc=0x196157b79485] (this=0x1b9a70990fa9 ,J=0x1ba6e8004101 ,am=0xb24ead25bf9 ) 2:日志 [console.js:~35] [pc=0x196157b784e8] (this=0x38b6577c5a19) 3:参数适配器框架:1...

我想将我的 JSON 保存到 mongodb,而不是使用输出文件。是否可以在不首先在 Node 中运行代码的情况下执行此操作?

【问题讨论】:

  • 请说得更具体一些。一小段代码可能

标签: json node.js mongodb


【解决方案1】:

有两种选择:您可以使用 Mongo Shell scripts 或 Node.js 脚本。

Mongo Shell 脚本可以直接在 Mongo Shell 中运行,也可以从文件中加载。

在 Node.js 脚本中,您可以使用 MongoDB packagesofficial driver 之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2011-11-23
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多