开发者

MongoDB Persisting mixed type array

I have a MongoDB and in one of the collections I am trying to save a array of objects. Some of these objects will be string, some bool, some decimal. The definition for the array (actually it is a Dictionary) is this:

    public Dictionary<string, object> Fields
    {
        get
      开发者_运维问答  {
            if (_Fields == null)
                _Fields = new Dictionary<string, object>();
            return _Fields;
        }
        set
        {
            _Fields = value;
        }
    }

Up to the point where my code saves the data to the database, the types of the variables in the array are correct. When I save and look at the data via either MongoHQ or in code, the array comes back with all strings.

I am using the C# Driver for this.

Is there a way to force a variable to a type? Am I missing something here?


What you are asking is reasonable. There's no special syntax when it comes to objects -- just make sure that your inputs aren't strings. However in the source code that I have (could be old), I see no mapping for a dictionary in the BSON serializer.

Now in BerkeleyDB you needed to define a callback for each type that you stored, but MongoDB through BSON is a little smarter than that.

btw: I'm using the https://github.com/atheken/NoRM driver for MongoDB. What are you using?

Ideas:

  • try a collection, list, or array instead of a dictionary

  • serialize/deserialize your dictionary to/from byte[]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜