0

I would like to get collection via string and insert many json via string. How to do it? I don`t want to prepare object via C# class because my schema will change all the time and this is easy way.

code

const string connectionString = "mongodb://user:password@localhost:27017/myDB";

var client = new MongoClient(connectionString);

var db = client.GetDatabase("myDB");

var col = db.GetCollection("mycollection")

string insert = "[("Element1":"Test"),("Element2":"Test")]";

col.insertMany(insert);

my assemblies

MongoDB.Bson
MongoDB.Driver
MongoDB.Driver.Core
MongoDB.Libmongocrypt
Rafał Developer
  • 2,135
  • 9
  • 40
  • 72
  • 1
    You can use the generic BsonDocument class. See e.g. https://stackoverflow.com/questions/18068772/mongodb-c-sharp-how-to-work-with-bson-document – Christoph Lütjen Apr 20 '20 at 15:19

1 Answers1

0

ok. I found solution

public static async Task SaveToCollation(IMongoDatabase database, string collectionName, string json)
        {
            var collection = database.GetCollection<BsonDocument>(collectionName);
            await collection.InsertOneAsync(BsonDocument.Parse(json));
        }
Rafał Developer
  • 2,135
  • 9
  • 40
  • 72