如何使用MongoDB C#客户端驱动程序将复杂的嵌套JSON数据结构保存到MongoDB



我在下面的data.JSON文件中保存了一个复杂的嵌套JSON数据结构。数据结构被简化,但这将适用于所有json数据结构。我读取json并将其反序列化为C#模型。然后,我使用MongoDB C#客户端驱动程序将对象/数据写入MongoDB如何将这个COMPLEX对象/数据/模型保存到MongoDB?

复杂的嵌套数据结构,data.json文件

[
{
"property_1": "value_1",
"property_2": "value_2",
"property_3": {
"_some_property_1": 1,
"_some_property_2": "some_value_2",
"some_property_3": "some_value_3",
"some_property_4": "some_value_4"
},
"property_4": "value_4",
"property_5": "value_5",
"iproperty_6": "Nvalue_6",
"property_7": "value_7",
"property_8": "value_8",
"property_9": "value_9"
}
]

C#ASP.NET Web Api演示代码

namespace DemoWebApi.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public void Get()
{
var models = CreateModels();
var client = new MongoClient("mongodb://localhost:27017/admin");
var database = client.GetDatabase("TestDB");
/*
THIS DOES NOT WORK
IMongoCollection<MODEL> collection = database?.GetCollection<MODEL>("TestCollection");            
collection.InsertMany(models);
*/
}
private static List<Model> CreateModels()
{
List<Model> models = new List<Model>();
foreach (string file in Directory.GetFiles("<path/to/data.json>", "*", SearchOption.AllDirectories))
{
using (var fileStream = File.OpenRead(file))
{
using (var sr = new StreamReader(fileStream))
{
string json = sr.ReadToEnd();
var data = JsonConvert.DeserializeObject<List<Model>>(json);
models.AddRange(data);
}
}
}
return models;
}
}
}

C#模型

namespace DemoWebApi.Models
{
public class Model
{
[BsonExtraElements, BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments), JsonExtensionData]
public IDictionary<string, object> _additionalData;
}
}

在团队成员的帮助下,这是可行的。将模型序列化为BsonDocument,并将BsonDocument传递给MongoDB C#驱动程序插入方法。这样你就可以使用你的C#POCO对象,直到你想把它持久化到MongoDB,然后你序列化到BsonDocument数组或BsonDocument并保存它。对于这个复杂的模型,BsonDocument[]解决了我的问题

将一个具有这种形状和结构的复杂json模型保存到MongoDB 中,该模型比下面的形状和结构更复杂

//Complex json collection/array shape and structure to save as is in MongoDB
[
{
"property_1": "value_1",
"property_2": "value_2",
"property_3": {
"_some_property_1": 1,
"_some_property_2": "some_value_2",
"some_property_3": "some_value_3",
"some_property_4": "some_value_4"
},
"property_4": "value_4",
"property_5": "value_5",
"iproperty_6": "Nvalue_6",
"property_7": "value_7",
"property_8": "value_8",
"property_9": "value_9"
}
]


// This works - code snippets
/*Create a collection of C# POCO from for the json.
// My assumption is you can create C# objects/collections from complex json shape and structure
//   Serialize to MongoDB BsonDocument and save*/
var models = new List<Model>{ new Model()}; // Create C# collection from json
string text = JsonConvert.SerializeObject(models);
var bsonDocument = BsonSerializer.Deserialize<BsonDocument[]>(text);
var client = new MongoClient("mongodb://localhost:27017/admin");
var database = client.GetDatabase("TestDB");
IMongoCollection<BsonDocument> collection = database?.GetCollection<BsonDocument>("TestCollection");
collection.InsertMany(bsonDocument);

最新更新