Search code examples
mongodbmongodb-compass

MongoDB compass its not exporting all data for collection


while trying to export collection from MongoDB compass it's not exporting all data, it's only export fields that are present in all documents. for eg: if document 1 has

{
    "Name": "Alex",
    "__v": 0
}

and if Document 2 has


{
    "Name": "Joe",
     "ID"  : 07
    "__v": 0
}

and when trying to export collection it's only exporting Name fields. I'm trying to export all fields through the MongoDB Compass. is there any other way to export all data through any code or script

EDIT: the solution is Update to new version of compass and while exporting data from mongo if the field name is not present in the list, there is an option to add a field through we can add a field that misses by compass


Solution

  • MongoDB Compass has known issues on exporting an importing data for long time and it seems they are not willing to improve it!

    When you try to export data using compass, it uses some sample documents to select the fields and if you are unlucky enough, you will miss some fields.

    SOLUTION:

    1. Use the Mongo DB Compass Aggregation tab to find all the existing fields in all documents:

      [{$project: { arrayofkeyvalue: { $objectToArray: '$$ROOT'} }},
      {$unwind: '$arrayofkeyvalue'},
      {$group: { _id: null, allkeys: { $addToSet: '$arrayofkeyvalue.k' } }}]

    2. Add the fields from the 1st step to the Export Full Collection (Select Fields).

    3. Export it!