Mondodb C#节省与数组值字典

时间:2019-02-01 19:25:41

标签: c# mongodb

我有一个mongo集合:Scaffold。如果嵌入对象BooksStreamFieldset具有列表。

Fieldset有一个Field我想对这本字典的价值有所了解

Field

这是我要输入的值的示例。某种键值集合

enter image description here

保存时,这是我在mongo中拥有的值

enter image description here

1 个答案:

答案 0 :(得分:0)

嗯。它像应该工作的那样工作>

static void Main(string[] args)
{
    var client = new MongoClient();
    var database = client.GetDatabase("SO3");
    var collection = database.GetCollection<Fieldset>("jobject");
    var id = new BsonObjectId(ObjectId.GenerateNewId()).ToString();
    var field =
        new Field
        {
            Config = new Dictionary<string, object> {{"value", "item1key"}, {"lable", "item1value"}}
        };
    var field2 =
        new Field
        {
            Config = new Dictionary<string, object> {{"value", "item2key"}, {"lable", "item2value"}}
        };
    var fieldset = new Fieldset
    {
        Id = id,
        Slug = "aa",
        Fields = new List<Field>
        {
            field,
            field2
        }
    };
    collection.InsertOne(fieldset);
}


{ "_id" : ObjectId("5c55b972ceb9443d385de936"), "Slug" : "aa", "Fields" : [ { "Config" : { "value" : "item1key", "lable" : "item1value" } }, { "Config" : { "value" : "item2key", "lable" : "item2value" } } ] }