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:
- Go to Aggregations
- Select: $match
- Copy your query inside the match
- 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
- 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:
- db.videos is the collection name (videos is the collection name)
- 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)
- $match is like find()
- $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.
Go to Aggregations and choose $sort.
Past your sort:
{ name: 1}
Save as Create a view.
Then you just need to go to that view and it will be sorted and ready to export.