How to export JSON from MongoDB using Robomongo

前端 未结 14 780
暗喜
暗喜 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条回答
  •  旧时难觅i
    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"
    }
    ]);
    

提交回复
热议问题