How to export JSON from MongoDB using Robomongo

前端 未结 14 765
暗喜
暗喜 2021-01-30 01:37

So I do not know much about MongoDB. I have RoboMongo using which I connect to a MongoDB. What I need to do is this - there is a collection in that Mon

14条回答
  •  日久生厌
    2021-01-30 01:49

    Expanding on Anish's answer, I wanted something I can apply to any query to automatically output all fields vs. having to define them within the print statement. It can probably be simplified but this was something quick & dirty that works great:

    var cursor = db.getCollection('foo').find({}, {bar: 1, baz: 1, created_at: 1, updated_at: 1}).sort({created_at: -1, updated_at: -1});
    
    while (cursor.hasNext()) {
        var record = cursor.next();
        var output = "";
        for (var i in record) {
          output += record[i] + ",";
        };
        output = output.substring(0, output.length - 1);
        print(output);
    }
    

提交回复
热议问题