C# 如何使用c在mongodb内部对象中进行聚合排序#

C# 如何使用c在mongodb内部对象中进行聚合排序#,c#,mongodb,c#-4.0,aggregation-framework,mongodb-.net-driver,C#,Mongodb,C# 4.0,Aggregation Framework,Mongodb .net Driver,我需要用mongo db对类的内部对象排序。我需要根据分数排序 示例Json: {"_id":"58e46f81c4734559ac8082f0","Name":"test","Students":[{"Name":"A","Marks":"988"}]} {"_id":"58e46f81

我需要用mongo db对类的内部对象排序。我需要根据分数排序

示例Json:

{"_id":"58e46f81c4734559ac8082f0","Name":"test","Students":[{"Name":"A","Marks":"988"}]}
{"_id":"58e46f81c4734559ac8082f1","Name":"sahir","Students":[{"Name":"sahir","Marks":"311"}]}
下面是课堂:

public class  Student
    {


        [BsonId]
        public ObjectId Id { get; set; }
    
        [BsonElement]
        public string Name { get; set; }
        
        public ICollection <Students> Students { get; set; }

    }

    public class Students
    {
    
        public string Name { get; set; }
        
        public string Marks{ get; set; }

    }
公共班级学生
{
[BsonId]
公共对象Id{get;set;}
[b单一元素]
公共字符串名称{get;set;}
公共ICollection学生{get;set;}
}
公立班学生
{
公共字符串名称{get;set;}
公共字符串标记{get;set;}
}
行动

public IActionResult Index()
    {


        //Get the database connection
        mongoDatabase = GetMongoDatabase();
        var x = mongoDatabase.GetCollection<Student>("student").Find(FilterDefinition<Student>.Empty).ToList();


  
        var _dbContext = mongoDatabase.GetCollection<Student>("student");

// mongo db document collection student class students objects need to sort based on marks.
        _dbContext.Aggregate<Student>()
                         .Sort(x=> x.students.marks).Skip((page-1)*pageSize).Limit(100);
                          
                          .ToList();         -- how to do that sort in this line 


or 


    var result = _dbContext.Aggregate<Student>().Sort(new BsonDocument("Marks", 1)).ToList();



        return View(result);
    }
public IActionResult Index()
{
//获取数据库连接
mongoDatabase=GetMongoDatabase();
var x=mongoDatabase.GetCollection(“学生”).Find(FilterDefinition.Empty.ToList();
var_dbContext=mongoDatabase.GetCollection(“学生”);
//mongo db文档集合学生类学生对象需要根据分数进行排序。
_dbContext.Aggregate()
.Sort(x=>x.students.marks)。跳过((第1页)*页面大小)。限制(100);
.ToList();--如何在这行中进行排序
或
var result=_dbContext.Aggregate().Sort(新的BsonDocument(“Marks”,1)).ToList();
返回视图(结果);
}
注意:

{"_id":"58e46f81c4734559ac8082f0","Name":"test","Students":[{"Name":"A","Marks":"988"}]}
{"_id":"58e46f81c4734559ac8082f1","Name":"sahir","Students":[{"Name":"sahir","Marks":"311"}]}
不提供linq解决方案来转换可查询运算符以执行linq查询。这个问题只是基于对MongoDB聚合排序以及如何在C#
中使用它的理解

没有论坛提供任何关于使用c#进行内部对象排序的参考。有很多c#mongodb排序而不加累加的示例

问题:
我有聚合排序mongo shell查询。。不知道如何在c#

中实现这一点,您是否正在尝试获取按分数排序的所有学生的列表?如果是,请尝试以下方法:

var students=wait collection.AsQueryable()
.SelectMany(c=>c.Students)
.OrderBy(s=>s.Marks)
.Skip(50)
.Take(10)
.ToListAsync();
获得所需结果的最简单方法是通过
AsQueryable()
接口

如果必须使用
Aggregate()
api,可以执行以下操作:

var students=wait collection.Aggregate()
.放松(c=>c.学生)
.替换为(“$Students”)
.Sort(“{Marks:1}”)
.Skip(0)
.限额(10)
.As()
.ToListAsync();
测试程序:

使用MongoDB.Driver;
使用MongoDB.Driver.Linq;
使用MongoDB.Entities;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
命名空间测试应用程序
{
公共课:实体
{
公共IEnumerable学生{get;set;}
}
公立班学生
{
公共字符串名称{get;set;}
公共字符串标记{get;set;}
}
公共静态类程序
{
专用静态异步任务Main()
{
等待DB.InitAsync(“测试”);
等待新的[]{
新课程
{
学生=新[]{
新学生{Name=“a”,Marks=“100”},
新学生{Name=“b”,Marks=“200”},
}
},
新课程
{
学生=新[]{
新学生{Name=“d”,Marks=“400”},
新学生{Name=“c”,Marks=“300”},
}
}
}.SaveAsync();
var students=wait DB.Queryable()
.SelectMany(c=>c.Students)
.OrderBy(s=>s.Marks)
.Skip(50)
.Take(10)
.ToListAsync();
}
}

}
不,我已经用linq转换了内存集合中的asquerable,我不想用linq,需要使用sort Aggregation框架,因为我们的框架是基于此设计的,并且完成了筛选,如果没有嵌套对象,则进行排序。但当内部嵌套有问题时。AsQueryable是在服务器端完成的,而不是在内存中。。。但是ok:-)对不起,我把ienumerable和asquerable混淆了。如果服务器端MongoDB没有该操作符,我将该集合转换为list,然后将Linq转换为asQuerable来执行该操作,如果我这样做,我们的基础框架将不允许它。因此,我不得不使用聚合框架..AsQueryable()在SQL Server级别进行筛选,并获取所需的任何记录。因此,它减少了内存需求并提高了应用程序的性能。请参阅我更新的答案。
AsQueryable()
端点在内存中不进行任何筛选。它只是将LINQ指令转换为mongo查询语言,并将其发送到db服务器。因此,您从mongo服务器得到的只是您要求的确切项目。我知道,并且我已经使用mongodb进行了筛选,这很好,只是排序中出现了问题,并且已经准备好了此查询。我不想使用LINQ翻译来翻译该查询。使用聚合运算符还有其他方法吗。
dbContext.Aggregate()。展开(x=>x.students)。排序(新的BsonDocument(“标记”,1))。跳过((第1页)*页面大小)。限制(100)。ToList()