6

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

Rahul_Mandhane
  • 187
  • 1
  • 11

3 Answers3

10

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!

SalmanShariati
  • 3,873
  • 4
  • 27
  • 46
  • this is absolutely ridiculous. They must add a note in the export dialog. – Sang Sep 01 '22 at 18:03
  • @Sang They have indeed added a note but the note sounds technical, something like "selected fields are just an sample of fields available in the collection. Please add any other field name that you would like" – veritas Aug 20 '23 at 09:25
  • Will this pick up all fields even in deep nested blocks within a document? – veritas Aug 20 '23 at 09:59
0

the solution is while exporting data from mongo if the field name is not present in the list, there is an option to add a field through which we can add a field that missed by compass.

Rahul_Mandhane
  • 187
  • 1
  • 11
0

Just adding nuance for novice users adopting from @salmanShariati's answer

Go to mongo shell (Mongosh) at the bottom screen of the Compass tool. choose your database: use databasename;

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

Answer you will receive are all your fields in the collection. E.g these are keys in my collection.

<

{ _id: null,
  allkeys: 
   [ 'somefield',
     'cID',
     'userID',
     'dData',
     'dID',
     '_class',
     '_id' ] }
veritas
  • 378
  • 1
  • 6
  • 16