I am using MongoDB.Drivers
in my C# MVC application to communicate with Mongodb database.
C# Code
var client = new MongoClient("mongodb://localhost:27012");
var db = client.GetDatabase("Test_DB");
var collection = db.GetCollection<BsonDocument>("TestTable");
var tData = await collection.FindAsync(new BsonDocument(true)); // I used - new BsonDocument(true) to specify allow duplicate element name while read data.
In above image you can see I have multiple Columns called DuplicateCol
with different values.
When I tried to read these data in c#
using MongoDB.Driver
I got following error : InvalidOperationException: Duplicate element name 'DuplicateCol'.
While insert duplicate element name I used AllowDuplicateNames=true
of BsonDocument
object as below. (It insert duplicate element name without error.)
BsonDocument obj = new BsonDocument();
obj.AllowDuplicateNames = true;
obj.Add("DuplicateCol", "Value_One");
obj.Add("propone", "newVal");
obj.Add("DuplicateCol", "Value_Two");
.... // other properties with value
await collection.InsertOneAsync(obj);
Note: This Schema is Must. I can not altered it.
Please provide me suggestions to fix this Issue. Any help would be highly appreciated..
Thanks.