S
S
sergeevpetro2016-04-21 15:52:48
MongoDB
sergeevpetro, 2016-04-21 15:52:48

How to export database from MongoDB to JSON file?

SUBJECT!
Here's how I do it:

string connectionString = "mongodb://localhost:27017";
MongoClient client = new MongoClient(connectionString);
IMongoDatabase database = client.GetDatabase("mybase");
IMongoCollection<User> collection = database.GetCollection<User>("Users");
StreamWriter sr = new StreamWriter("JSON.txt");
var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
sr.Write(collection.ToJson(jsonWriterSettings));
sr.Close();
MessageBox.Show("ГОТОВО");

The following hat is written to the file:
{ "_t" : "MongoCollectionImpl`1" }

HELP!!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
andy-pro, 2016-04-21
@sergeevpetro

the mongo package includes an export utility , everything is very simple:
mongoexport --db test --collection traffic --out traffic.json

A
Alexander Ananiev, 2016-04-21
@SaNNy32

You need to loop through the collection and write to the file in the loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question