I need to sort inner object sort of a class with mongo db. i need to sort based on marks
Sample Json:
{"_id":"58e46f81c4734559ac8082f0","Name":"test","Students":[{"Name":"A","Marks":"988"}]}
{"_id":"58e46f81c4734559ac8082f1","Name":"sahir","Students":[{"Name":"sahir","Marks":"311"}]}
Here is the class:
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; }
}
Action
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);
}
Note:
Don't provide linq Solution to convert asQuerable operators to do LINQ queries. This question is only based on an understanding of MongoDB aggregation sort and how to use that in C#
.
No forum has any reference for internal objects sorting with c#.There are lots of examples of C# mongodb sorting without aggragtion
Problem:
I have aggregation sort mongo shell query .. don't know to implement that in c#
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…