Вопрос по c#-4.0, mongodb – Есть ли в Mongodb C # драйвер поддержки System.Dynamic.DynamicObject в .NET 4?

17

Error: User Rate Limit Exceeded

@{
    var feeds = DP.Database.GetCollection("feeds").FindAll();
}
<ul>
    @foreach (dynamic feed in feeds)
    {
        <li>@feed.message - @feed.from.name</li>
    }
</ul>

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Ваш Ответ

5   ответов
14

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededgroups.google.com/forum/?fromgroups#!topic/mongodb-user/… hoang
Error: User Rate Limit ExceededMongoDB.DynamicError: User Rate Limit Exceeded
Error: User Rate Limit Exceededmongodb.github.io/mongo-csharp-driver/2.0/what_is_new/#new-api
Error: User Rate Limit Exceeded
17

dynamic obj = collection.FindOneByIdAs<BsonDocument>(someObjectId).ToDynamic();

public static class MongoDynamic
{
    private static System.Text.RegularExpressions.Regex objectIdReplace = new System.Text.RegularExpressions.Regex(@"ObjectId\((.[a-f0-9]{24}.)\)", System.Text.RegularExpressions.RegexOptions.Compiled);
    /// <summary>
    /// deserializes this bson doc to a .net dynamic object
    /// </summary>
    /// <param name="bson">bson doc to convert to dynamic</param>
    public static dynamic ToDynamic(this BsonDocument bson)
    {
        var json = objectIdReplace.Replace(bson.ToJson(), (s) => s.Groups[1].Value);
        return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
    }
}

)

Error: User Rate Limit Exceededbson.ToJson(new JsonWriterSettings {OutputMode = JsonOutputMode.Strict})Error: User Rate Limit Exceeded
0

public static class MongoDynamic
{
    /// <summary>
    /// deserializes this bson doc to a .net dynamic object
    /// </summary>
    /// <param name="bson">bson doc to convert to dynamic</param>
    public static dynamic ToDynamic(this BsonDocument bson)
    {
        var json = bson.ToJson(new MongoDB.Bson.IO.JsonWriterSettings { OutputMode = JsonOutputMode.Strict });
        dynamic e =  Newtonsoft.Json.JsonConvert.DeserializeObject<ExpandoObject>(json);
        BsonValue id;
        if (bson.TryGetValue("_id", out id))
        {
            // Lets set _id again so that its possible to save document.
            e._id = new ObjectId(id.ToString());
        }
        return e;
    }
}

// Get BsonDocument from db here
BsonDocument doc = ...

// Convert to dynamic.
var d = doc.ToDynamic();

// Lets add a none existant property.
d.Name = "test";

// Assuming you already have your collection set up
collection.Save(new BsonDocument(d));
5

map.MapProperty(member => member.Data)
   .SetElementName("Data")
   .SetSerializer(new DynamicSerializer());

[BsonSerializer(typeof(DynamicSerializer))]
public dynamic Data { get; set; }

public class DynamicSerializer : IBsonSerializer
{
  #region Implementation of IBsonSerializer

  public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
  {
    return Deserialize(bsonReader, nominalType, null, options);
  }

  public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType,
    IBsonSerializationOptions options)
  {
    if (bsonReader.GetCurrentBsonType() != BsonType.Document) throw new Exception("Not document");
    var bsonDocument = BsonSerializer.Deserialize(bsonReader, typeof(BsonDocument), options) as BsonDocument;
    return JsonConvert.DeserializeObject<dynamic>(bsonDocument.ToJson());
  }

  public IBsonSerializationOptions GetDefaultSerializationOptions()
  {
    return new DocumentSerializationOptions();
  }

  public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
  {
    var json = (value == null) ? "{}": JsonConvert.SerializeObject(value);
    BsonDocument document = BsonDocument.Parse(json);
    BsonSerializer.Serialize(bsonWriter, typeof(BsonDocument), document,options); 
  }

  #endregion
}

Error: User Rate Limit Exceeded
4

    /// <summary>
    /// deserializes this BsonDocument cursor result to a list of .net dynamic objects
    /// </summary>
    /// <param name="cursor">cursor result to convert to dynamic</param>
    /// <returns></returns>
    public static List<dynamic> ToDynamicList(this MongoCursor<BsonDocument> cursor)
    {
        var dynamicList = new List<dynamic>();
        var list = cursor.ToList();
        for (int i = 0, l = list.Count; i < l; i++)
            dynamicList.Add(list[i].ToDynamic());

        return dynamicList;
    }
Error: User Rate Limit Exceededcursor.Select(doc => doc.ToDynamic())Error: User Rate Limit Exceeded.ToList()Error: User Rate Limit Exceeded

Похожие вопросы