0

db(database).listCollection().toArray(function (error, isListCollection){if(error){response.json(error.message)}else if(isListCollection){for(var i = 0; i < isListCollection.length; i++){const listColl = isListcoll.name; db(database).collection(listColl, (error, data) => { if(errr){responden.json(error.message)}else if(data){respons.json(data)});}

How to loop and push the respon data to Json file. Thanks

Ahmed
  • 55
  • 2
  • 9

1 Answers1

0

Are you trying to take backup? If yes there is a command mongoexport you can use to get response directly in json format. It is provided by mongo

Garry
  • 536
  • 1
  • 5
  • 11
  • Yes but , i'm wanna encrypt the bson data and write to custom file – Ahmed May 27 '19 at 16:07
  • Not sure why you want to do all of this as bson is not in readable form. Moreover you can protect the backed up file by adding username/password/Private key. – Garry May 27 '19 at 17:27
  • Could i'm run mongoexport using `db(database).adminCommand()` ? – Ahmed May 27 '19 at 22:42
  • Can you explain me the situation what you are trying to achieve? Do you want to take backup with script or you want to use mongo shell? – Garry May 28 '19 at 16:07
  • First i'm get list collection and than i'm get data per collection and push it to custom file . Example `"data": ["collectionName": [this is respon json data collectionName], "collectionNames": [respon json data collectionNames], ... ]` per respon collection Json day i'm wanna push to `"data": [push here]` in my `database.custom` file . After that i'm encrypt all data on `database.custom` with my encryption. Thanks – Ahmed May 29 '19 at 00:18
  • check this https://stackoverflow.com/questions/11255630/how-to-export-all-collection-in-mongodb/25694009 hopefully you would get your response. – Garry May 29 '19 at 15:04