【问题标题】:NodeJS access variable from mongodb & write to JSON fileNodeJS 从 mongodb 访问变量并写入 JSON 文件
【发布时间】:2016-11-24 09:09:54
【问题描述】:

我需要将 mongodb 数据库中的字段写入 json 文件。使用节点 js。怎么办?我没有发现任何有用的东西。

【问题讨论】:

    标签: json node.js mongodb


    【解决方案1】:

    试试这个

    var exportDocuments = function (db, callback) {
        // Get the documents collection 
        var collection = db.collection('documents');
        // Find some documents 
        collection.find({}).toArray(function (err, docs) {
            require('fs').writeFile('yourFileName.json', JSON.stringify(docs), function (error) {
                if (error) return callback(error);
                callback();
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 2017-10-05
      • 1970-01-01
      • 2012-11-13
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多