mongoose aggregate()不会返回正确的值

时间:2016-03-23 08:37:38

标签: mongodb mongoose

我是Mongodb aggregate()的新手。当我从Mongo shell尝试它时,它工作得很好,如下面的代码:

   public class MainClass
   {
        public MainClass()
        {
            _class2ListProperty = new List<Class2>();
            _class1ListProperty = new List<Class1>();
        }

        private List<Class2> _class2ListProperty = new List<Class2>();
        private List<Class1> _class1ListProperty = new List<Class1>();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<Class2> Class2ListProperty 
        { 
            get
            {
                return _class2ListProperty;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<Class1> Class1ListProperty 
        {
            get
            {
                return _class1ListProperty;
            }
        }
    }

    public class Class1
    {
        private Class2 _class2Member;

        public String Name { get; set; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Class2 Class2Property 
        {
            get
            {
                return _class2Member;
            }
            set 
            {
                this._class2Member = value;
            } 
        }
    }


    public class Class2
    {
        public Class2()
        {
            _class3ListMember = new List<Class3>();
        }

        private List<Class3> _class3ListMember = new List<Class3>();

        public String Name { get; set; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<Class3> Class3ListProperty 
        {
            get 
            { 
                return _class3ListMember; 
            }
        }
    }

    public class Class3
    {
        public Class3()
        {
            _class3ListMember = new List<Class3>();
        }

        public String Name { get; set; }

        [TypeConverter(typeof(Int32RangeConverter))]
        public Int32Range Runtime { get; set; }

        private List<Class3> _class3ListMember = new List<Class3>();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<Class3> Class3ListProperty 
        { 
            get 
            { 
                return _class3ListMember; 
            } 
        }
    }

但是,当我将相同的聚合操作移动到由Mongoose支持的服务器时,

> db.trics.aggregate( [ {$group: {_id: "$club", count: {$sum: 1}}}, {$sort: {count: -1}}, {$limit: 1} ] )
{ "_id" : 0, "count" : 2 }

$ group结果的“_id”总是返回“null”:我从邮递员那里得到了什么

Object.aggregate([ {$group: {_id: "$club", count: {$sum: 1}}} ]).sort('-count').limit(1)

任何Mongo / Mongoose专家都可以帮我解决这个问题吗?

非常感谢

仅供参考:“对象”文档示例:

[ { _id: null, count: 2 } ]

0 个答案:

没有答案