Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export sorted data using mongoexport?

Tags:

mongodb

I have a collection in mongo which has name and count fields.

{name:'myName',count:5}

Is it possible to sort data by count and export as json using mongoexport?

like image 936
Parvin Gasimzade Avatar asked Jan 25 '12 16:01

Parvin Gasimzade


2 Answers

Starting with MongoDB 2.6, you can pass --sort to mongoexport directly:

mongoexport --db mydatabase -c people --fields name,age --sort "{name: 1, age: 1}"
like image 180
Dan Dascalescu Avatar answered Oct 21 '22 03:10

Dan Dascalescu


Actually you can, but you have to use special params. Here's a sample (redirecting to file):

$ mongoexport  -q '{ $query: {count: {$gt:0}}, $orderby: {count: -1} }' -d database -c collection > data_dump.json

The $query part is not strictly necessary, but I included it because of this bug on GitHub (fixed now, but only just).

like image 39
rowanu Avatar answered Oct 21 '22 01:10

rowanu