使用c#驱动程序将字典插入MongoDB [英] Insert Dictionary into MongoDB with c# driver

查看:344
本文介绍了使用c#驱动程序将字典插入MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于一种我无法预测我的MongoDB文档将要有哪些字段的情况。所以我不能再创建一个 _id 类型为 BsonID 的对象。
我发现创建一个字典(HashTable)非常方便,并将我的DateTime和String对象添加到我需要的次数。



然后我尝试将生成的Dictionary对象插入到MongoDb中,但默认序列化失败。



这是我的对象的类型HashTable(像一个字典,但内部有多种类型):

  {_id:,
metadata1:asaad,
metadata2:[],
metadata3:ISODate(somedatehere)}

我得到的驱动程序的错误是:


Serializer DictionarySerializer预期的序列化类型为DictionarySerializationOptions的选项,而不是DocumentSerializationOptions


我google了,但找不到任何有用的东西。我做错了什么?

解决方案

司机需要找到_id字段。您可以创建一个只有两个属性的C#类:Id和值。

  public class HashTableDocument 
{
public ObjectId Id {get;组; }
[BsonExtraElements]
public Dictionary< string,object>价值{get;组; }

}

请注意,我们必须使用Dictionary< string,object> ;而不是Hashtable。



然后,您可以使用以下代码插入文档:

  var document = new HashTableDocument 
{
Id = ObjectId.GenerateNewId(),
值=新字典< string,object>
{
{metadata1,asaad},
{metadata2,new object [0]},
{metadata3,DateTime.UtcNow}
}
};
collection.Insert(document);

我们可以使用MongoDB shell来确认插入的文档具有所需的形式:

 > db.test.find()。pretty()
{
_id:ObjectId(518abdd4e447ad1f78f74fb1),
metadata1:asaad,
metadata2 :[],
metadata3:ISODate(2013-05-08T21:04:20.895Z)
}
>


I am in a situation where I can't predict which fields my MongoDB document is going to have. So I can no longer create an object with an _id field of type BsonID. I find it very convenient to create a Dictionary (HashTable) and add my DateTime and String objects inside, as many times as I need it.

I then try to insert the resulting Dictionary object into MongoDb, but default serialization fails.

Here's my object of Type HashTable (like a Dictionary, but with varied types inside):

{ "_id":"",
"metadata1":"asaad",
"metadata2":[],
"metadata3":ISODate("somedatehere")}

And the error from the driver I get is:

Serializer DictionarySerializer expected serialization options of type DictionarySerializationOptions, not DocumentSerializationOptions

I googled it, but couldn't find anything useful. What am I doing wrong?

解决方案

The driver needs to be able to find the _id field. You could create a C# class that has just two properties: Id and Values.

public class HashTableDocument
{
    public ObjectId Id { get; set; }
    [BsonExtraElements]
    public Dictionary<string, object> Values { get; set; }

}

Note that we have to use Dictionary<string, object> instead of Hashtable.

You could then use code like the following to insert a document:

var document = new HashTableDocument
{
    Id = ObjectId.GenerateNewId(),
    Values = new Dictionary<string, object>
    {
        { "metadata1", "asaad" },
        { "metadata2", new object[0] },
        { "metadata3", DateTime.UtcNow }
    }
};
collection.Insert(document);

We can use the MongoDB shell to confirm that the inserted document has the desired form:

> db.test.find().pretty()
{
        "_id" : ObjectId("518abdd4e447ad1f78f74fb1"),
        "metadata1" : "asaad",
        "metadata2" : [ ],
        "metadata3" : ISODate("2013-05-08T21:04:20.895Z")
}
>

这篇关于使用c#驱动程序将字典插入MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆