使用MongoDB C#驱动程序命令文档字段包含继承的类型对象

时间:2016-07-26 17:18:57

标签: c# asp.net-mvc mongodb c#-4.0 bson

我在C#中有以下两个类,我在Mongo Collection中使用BsonElement作为属性顺序。但是Car集合没有实现这个订单。

这里的想法是在BaseEntity类中使用这个公共字段并在几乎所有类上继承它

public class Car: BaseEntity{
   [BsonElement(Order = 1)]
   public string Color{ get; set; }
   [BsonElement(Order = 2)]
   public long Price{ get; set; }
}

public class BaseEntity{
   [BsonElement(Order = 99)]
   public DateTime DateAdded{ get; set; }
   [BsonElement(Order = 100)]
   public bool IsActive{ get; set; }
}

Mongo的汽车收集结果如下。基类属性首先出现在文档和子类中,尽管我有BsonElement(Order=xx)

{
   "_id" :ObjectId(fsdfsdfsdfsdfs),
   "DateAdded": ISO("SomeDate"),
   "IsActive" : true,
   "Color" : "Red",
   "Price" : NumberLong(23564)
}

我想把它看起来像

{
   "_id" :ObjectId(fsdfsdfsdfsdfs),
   "Color" : "Red",
   "Price" : NumberLong(23564)
   "DateAdded": ISO("SomeDate"),
   "IsActive" : true
}

驱动程序详细信息

  <package id="mongocsharpdriver" version="2.2.4" targetFramework="net461" />
  <package id="MongoDB.Bson" version="2.2.4" targetFramework="net461" />
  <package id="MongoDB.Driver" version="2.2.4" targetFramework="net461" />
  <package id="MongoDB.Driver.Core" version="2.2.4" targetFramework="net461" />

1 个答案:

答案 0 :(得分:0)

也许它不会落入预期的答案,但是在类中使用的属性在那里得到尊重,这意味着序列化程序首先使用它的基本元素构建最终类,然后使用具体对象中的元素。

为了测试,我创建了class bus:car

public class Bus:Car{
[BsonElement(Order = 1)]
   public string BusColor{ get; set; }
   [BsonElement(Order = 2)]
   public long BusPrice{ get; set; }
}

并且输出刚确认:

{
    "_id" : ObjectId("5799eb7c1751a60613b36cbc"),
    "DateAdded" : ISODate("2016-07-28T11:24:44.222Z"),
    "IsActive" : true,
    "Color" : "Black-White",
    "Price" : NumberLong(923),
    "BusColor" : "Blue",
    "BusPrice" : NumberLong(12345)
}

你要用排序解决什么问题?

欢迎任何评论!

==========

修改

发现在bson驱动程序源代码中

 private void OrderMembers()
    {
        // only auto map properties declared in this class (and not in base classes)
        var hasOrderedElements = false;
        var hasUnorderedElements = false;
        foreach (var memberMap in _declaredMemberMaps)
        {
            if (memberMap.Order != int.MaxValue)
            {
                hasOrderedElements |= true;
            }
            else
            {
                hasUnorderedElements |= true;
            }
        }

        if (hasOrderedElements)
        {
            if (hasUnorderedElements)
            {
                // split out the unordered elements and add them back at the end (because Sort is unstable, see online help)
                var unorderedElements = new List<BsonMemberMap>(_declaredMemberMaps.Where(pm => pm.Order == int.MaxValue));
                _declaredMemberMaps.RemoveAll(m => m.Order == int.MaxValue);
                _declaredMemberMaps.Sort((x, y) => x.Order.CompareTo(y.Order));
                _declaredMemberMaps.AddRange(unorderedElements);
            }
            else
            {
                _declaredMemberMaps.Sort((x, y) => x.Order.CompareTo(y.Order));
            }
        }
    }