Rahul_Mandhane
Rahul_Mandhane

Reputation: 352

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

Upvotes: 6

Views: 6403

Answers (3)

veritas
veritas

Reputation: 432

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' ] }

Upvotes: 0

Rahul_Mandhane
Rahul_Mandhane

Reputation: 352

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.

Upvotes: 0

SalmanShariati
SalmanShariati

Reputation: 3893

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!

Upvotes: 10

Related Questions