[英]Insert JSON into an existing MongoDB collection
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: 现在我有一个JSON文件如下:
{
"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. 您能否告诉我如何使用我的c#代码中的上述JSON文件更新customerId = 100的地址数组。
Please suggest.! 请建议。!
Thanks in advance :) 提前致谢 :)
I am writing a C# (C sharp)(.Net) code to insert a JSON file in mongoDB. 我正在编写一个C#(C sharp)(。Net)代码来在mongoDB中插入一个JSON文件。 i have a jsonfile " records.JSON " which has multiple document in one single row in it, like :
我有一个jsonfile“records.JSON”,它在一行中有多个文档,如:
[{"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. 我需要将此JSON文件插入到现有的MongoDB集合中。 So far I have the following code to connect and insert to mongodb :
到目前为止,我有以下代码连接并插入到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);
但是这给了我错误:“数组不能写入BSON文档的根级别”并且如果我将BSON解析为:
var bsonDoc = BsonDocument.Parse (text);
it gives me error as : Cannot deserialize BsonDocumet from BsonType Array.
它给出了我的错误:
Cannot deserialize BsonDocumet from BsonType Array.
Can anybody Please help me to understand How do i insert the JSON file into the mongoDB Collection. 任何人都可以帮我理解如何将JSON文件插入mongoDB集合。 ??
??
Any help is appreciated.. Thanks in advance. 任何帮助表示赞赏..在此先感谢。
Supposing that you are have a valid JSON file. 假设您有一个有效的JSON文件。
This is what you need to do, using the new MongoDB.Driver 2.0: 这是你需要做的,使用新的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. 正如之前的回答者所建议的那样,您的JSON格式无效。
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: 如果你将你的JSON传递给像这样的验证器: http : //jsonformatter.curiousconcept.com/你会看到问题与这里显示的不必要的括号有关:
[
{
"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"
}
]
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.