How to export JSON from MongoDB using Robomongo

前端 未结 14 761
暗喜
暗喜 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:42

    An extension to Florian Winter answer for people looking to generate ready to execute query.

    drop and insertMany query using cursor:

    {
        // collection name
        var collection_name = 'foo';
    
        // query
        var cursor = db.getCollection(collection_name).find({});
    
        // drop collection and insert script
        print('db.' + collection_name + '.drop();');
        print('db.' + collection_name + '.insertMany([');
    
        // print documents
        while(cursor.hasNext()) {
            print(tojson(cursor.next()));
    
            if (cursor.hasNext()) // add trailing "," if not last item
                print(',');
        }
    
        // end script
        print(']);');
    }
    

    Its output will be like:

    db.foo.drop();
    db.foo.insertMany([
    {
        "_id" : ObjectId("abc"),
        "name" : "foo"
    }
    ,
    {
        "_id" : ObjectId("xyz"),
        "name" : "bar"
    }
    ]);
    
    0 讨论(0)
  • 2021-01-30 01:43

    You can use tojson to convert each record to JSON in a MongoDB shell script.

    Run this script in RoboMongo:

    var cursor = db.getCollection('foo').find({}, {});
    while(cursor.hasNext()) {
        print(tojson(cursor.next()))
    }
    

    This prints all results as a JSON-like array.

    The result is not really JSON! Some types, such as dates and object IDs, are printed as JavaScript function calls, e.g., ISODate("2016-03-03T12:15:49.996Z").

    Might not be very efficient for large result sets, but you can limit the query. Alternatively, you can use mongoexport.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2021-01-30 01:54

    There are a few MongoDB GUIs out there, some of them have built-in support for data exporting. You'll find a comprehensive list of MongoDB GUIs at http://mongodb-tools.com

    You've asked about exporting the results of your query, and not about exporting entire collections. Give 3T MongoChef MongoDB GUI a try, this tool has support for your specific use case.

    0 讨论(0)
  • 2021-01-30 01:56

    Robomongo's shell functionality will solve the problem. In my case I needed couple of columns as CSV format.

    var cursor = db.getCollection('Member_details').find({Category: 'CUST'},{CustomerId :1,Name :1,_id:0})
    
    while (cursor.hasNext()) {
        var record = cursor.next();   
        print(record.CustomerID + "," + record.Name)
    }
    
    Output : -------
    
    334, Harison
    433, Rechard
    453, Michel
    533, Pal
    
    0 讨论(0)
  • 2021-01-30 01:57

    you say "export to file" as in a spreadsheet? like to a .csv?

    IMO this is the EASIEST way to do this in Robo 3T (formerly robomongo):

    1. In the top right of the Robo 3T GUI there is a "View Results in text mode" button, click it and copy everything

    2. paste everything into this website: https://json-csv.com/

    3. click the download button and now you have it in a spreadsheet.

    hope this helps someone, as I wish Robo 3T had export capabilities

    0 讨论(0)
提交回复
热议问题