I understood my mistake :) Thanks guys. I have one more Question, suppose i have multiple documents with the below structure in "Customer" collection.
{
"customerId":100,
"FirstName":"xyz",
"lastname":"pqr",
"address":[
{
"house":44,
"city":"Delhi",
"country":"india"
}
],
"employer":[
{
"cmpName":"ABC",
"type":"IT"
}
]
Now i have a JSON file as below:
{
"customerId":100,
"address":[
{
"house":99,
"city":"MUMBAI",
"country":"INDIA"
}
]
}
Can you please tell me how can i update the address array for customerId = 100 using the above JSON file in my c# code.
Please suggest.!
Thanks in advance :)
I am writing a C# (C sharp)(.Net) code to insert a JSON file in mongoDB. i have a jsonfile " records.JSON " which has multiple document in one single row in it, like :
[{"customerId" : 100,"FirstName" : "xyz","lastname" : "pqr","address":[{"house": 44,"city" : "Delhi", "country" : "india"}],"employer":[{"cmpName" : "ABC","type" : "IT"}]}][{"customerId" : 101,"FirstName" : "LMN","lastname" : "GHI","address":[{"house": 90,"city" : "NewYork", "country" : "US"}],"employer":[{"cmpName" : "ABC","type" : "IT"}]}]
I need to insert this JSON file into an existing MongoDB collection. So far I have the following code to connect and insert to mongodb :
public static void Main (string[] args)
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
server.Connect();
var database = server.GetDatabase("test");
var collection = database.GetCollection<BsonDocument>("test_collection");
string text = System.IO.File.ReadAllText(@"records.JSON");
var bsonDoc = BsonArray.Parse (text);
collection.Insert (bsonDoc);
}
But this is giving me error as : " an array cannot be written to root level of BSON document"
And if i parse BSON as : var bsonDoc = BsonDocument.Parse (text);
it gives me error as : Cannot deserialize BsonDocumet from BsonType Array.
Can anybody Please help me to understand How do i insert the JSON file into the mongoDB Collection. ??
Any help is appreciated.. Thanks in advance.
Open the Import Wizard. Then, choose JSON as the import format and click OK. Click on + to add JSON source documents, – to remove them, or the clipboard icon to paste JSON data from the clipboard. Here we will add the JSON source document, Rainfall-Data.
The process to import JSON into MongoDB depends on the operating system and the programming language you are using. However, the key to importing is to access the MongoDB database and parsing the file that you want to import. You can then go through each document sequentially and insert into MongoDB.
In MongoDB, the insert() method inserts a document or documents into the collection. It takes two parameters, the first parameter is the document or array of the document that we want to insert and the remaining are optional. Using this method you can also create a collection by inserting documents.
You can store JSON documents in SQL Server or SQL Database and query JSON data as in a NoSQL database. This article describes the options for storing JSON documents in SQL Server or SQL Database.
Supposing that you are have a valid JSON file.
This is what you need to do, using the new MongoDB.Driver 2.0:
public static void Main (string[] args)
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var database = client.GetDatabase("test");
string text = System.IO.File.ReadAllText(@"records.JSON");
var document = BsonSerializer.Deserialize<BsonDocument>(text);
var collection = database.GetCollection<BsonDocument>("test_collection");
await collection.InsertOneAsync(document);
}
I hope it works for you!
Regards.
As the previous answerer suggested, your JSON format is not valid.
If you pass in your JSON into a validator like this one: http://jsonformatter.curiousconcept.com/ you'll see that the issue has to do with unecessary brackets shown here:
[
{
"customerId":100,
"FirstName":"xyz",
"lastname":"pqr",
"address":[
{
"house":44,
"city":"Delhi",
"country":"india"
}
],
"employer":[
{
"cmpName":"ABC",
"type":"IT"
}
]
}
][ <-------------------- Delete these brackets
{
"customerId":101,
"FirstName":"LMN",
"lastname":"GHI",
"address":[
{
"house":90,
"city":"NewYork",
"country":"US"
}
],
"employer":[
{
"cmpName":"ABC",
"type":"IT"
}
]
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With