Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb Compass export data with sort

I am able to sort data in compass, but my problem is when I export, I don't get it sorted and I can't find where can I apply sort upon exporting data?

As you can see here I am filtering with {name: 'SelectedProduct'} and I am sorting with {name: 1}: Filtering and Sorting

But in the export collection dialog for the same filter and sort as above image, I see this: Export Dialog

I can't seem to find how can I apply sort on exported data.

like image 728
Ali Elkhateeb Avatar asked Aug 16 '18 03:08

Ali Elkhateeb


3 Answers

Even I am facing similar issue. If you see in the Query window in 1st screen you have got only 1 document where as in Export data window it is showing 984 records. This is a bug which needs to be fixed by MongoDB Compass. Export behaves like a "Entire Full Collection" & there is no impact of Query that is written.

like image 152
thejesh Avatar answered Oct 18 '22 22:10

thejesh


I also needed that.

  1. Go to Aggregations and choose $sort.

  2. Past your sort:

    {
      name: 1
    }
    
  3. Save as Create a view.

  4. Then you just need to go to that view and it will be sorted and ready to export.

like image 4
Eduardo Capela Avatar answered Oct 18 '22 21:10

Eduardo Capela


I found a solution (detour) to compass bug: steps:

  1. Go to Aggregations enter image description here
  2. Select: $match
  3. Copy your query inside the match
  4. Save as a new view (which actually creates a new collection with your results) - there is a bottom down arrow right to the save button
  5. Now go to this new collection and export it

Also if you want to do the same procedure through the shell this is the command: db.videos.aggregate([{$match:{hasError: {$ne: true},videos:{$gt:[]},businessAuth: {$in: [ObjectId('5f78a94323614f0020554f96')]}}},{ $out : "heavy" }])

explain:

  1. db.videos is the collection name (videos is the collection name)
  2. aggregate is a command which creates a pipeline with multiple orders such as query ($match) and output the results ($out) - it takes array of commands)
  3. $match is like find()
  4. $out is what to do with the results - so i asked to insert all the documents found by the query to a new collection (or exists one) named "heavy"
like image 3
dang Avatar answered Oct 18 '22 21:10

dang