Mongodb Compass export data with sort
Asked Answered
R

3

12

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.

Reareace answered 16/8, 2018 at 3:24 Comment(1)
Facing the same issue, not sure how to fix it.Maser
C
7

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.

Carousel answered 23/12, 2020 at 3:0 Comment(0)
L
6

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.

Leafage answered 20/3, 2019 at 5:31 Comment(1)
I also see a bug inside the query preview of compass (after you press on the export button) my query is displayed wrong (instead of ObjectId("fdfdffdgdf4435345") i see {id:new Buffer('_x\ufffdC#aO\u0000 UO\ufffd')}}Forewing
F
4

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"
Forewing answered 5/10, 2020 at 16:14 Comment(1)
This is a full solution to the issue and is a better explanation. It allows for the filtering, project, sorting and any other MQL features to be applied and then exporting the results.Bullbat

© 2022 - 2024 — McMap. All rights reserved.