Custom Search

Wednesday, May 8, 2019

How to Redirect output of mongoDB query to a csv file

a)
$ vim export_to_cv.js

print("id,city")
cursor = db.media.find();
while (cursor.hasNext()) {
    jsonObject = cursor.next();
    print(jsonObject._id.valueOf() + "," + jsonObject.city)

}

b)
mongo mediadb export_to_cv.js

mongo [host]/[dbname] -u [username] -p [password] export_to_cv.js > output.csv

No comments:

Post a Comment