Click here to Skip to main content
15,891,688 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I want to insert a JSON file but it's showing that duplicate _id cannot be inserted in MongoDB . I have json input file in below format
{"Name":"Name1","age": 21,..................}
{"Name":"Name1","age": 21,..................}
can any one help ?

What I have tried:

<pre> var client = new MongoClient();
                IMongoDatabase db = client.GetDatabase("TestDB1");
                string text = System.IO.File.ReadAllText(@"D:\DataMorph\Check.json");
                //var document = BsonSerializer.Deserialize<BsonDocument>(text);
                //var collection = db.GetCollection<BsonDocument>(collectionName);
                BsonDocument document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(text);
                var collection = db.GetCollection<BsonDocument>("data");               
                collection.InsertOne(document);
Posted
Comments
Richard MacCutchan 14-Dec-18 4:04am    
The error message is telling yo that you cannot have two records with the same id. You need to fix the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900