Mongodb Compass export data with sort Mongodb Compass export data with sort mongodb mongodb

Mongodb Compass export data with sort


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.


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

  1. Go to Aggregationsenter 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"


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.