0

I have a collection in a database, I want to export it to a json file in c#. I know the command that can be executed through the mongo shell. Has this command - mongoexport --collection = movies --db = director --out = movies.json

But I'm looking for the c # code to do that. Would appreciate help.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Both `System.Text.Json` and `Newtonsoft.Json` have methods for serialising objects as JSON - for example https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0. Once you have got your collection using the MongoDB C# driver, simply use one of those. Then when you have the JSON, you can write it to a file using `System.IO`. – bfren May 07 '21 at 08:16
  • You can run the `mongoexport` as a system command from C# as in this post: https://stackoverflow.com/questions/36256114/how-to-execute-a-command-and-get-its-output – prasad_ May 07 '21 at 08:38

0 Answers0