Redirect output of mongo query to a csv file

后端 未结 7 826
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 06:01

I am using MongoDB 2.2.2 for 32-bit Windows7 machine. I have a complex aggregation query in a .js file. I need to execute this file on the shell and direct the output to a C

7条回答
  •  借酒劲吻你
    2020-12-02 06:44

    Here is what you can try:

    print("id,name,startDate")
    cursor = db..find();
    while (cursor.hasNext()) {
        jsonObject = cursor.next();
        print(jsonObject._id.valueOf() + "," + jsonObject.name + ",\"" + jsonObject.stateDate.toUTCString() +"\"")
    
    }
    

    Save that in a file, say "export.js". Run the following command:

    mongo / -u  -p  export.js > out.csv
    

提交回复
热议问题