在 MongoDB C# 上获取上一个、当前、下一个
我有一个学生集合,它将注册学生数据存储在 MongoDB 中。它有 StudentName、SaveDate 等字段。我正在通过 .net 框架 WebAPI 将详细信息显示到我的网站。 我想要做的是按照 SaveDate 的顺序获取上一个学生 ID 的 StudentDetails 。
我当前的查询从下面的查询中按学生 ID 获取学生详细信息,
var collection = StudentDB.Collection<Student>("StudyMaterials");
var curfilter = Builders<Student>.Filter.Eq(x => x.studentID, studentID);
studentDetails= collection.Find(curfilter).FirstOrDefault();
这是我的 Student.cs 类,
public class Student
{
[BsonRepresentation(BsonType.String)]
public Guid StudentID {get; set;}
public string StudentName {get; set;}
[BsonRepresentation(BsonType.String)]
public Guid NextStudentID {get; set;}
[BsonRepresentation(BsonType.String)]
public Guid PrevStudentID {get; set;}
}
我想使用聚合,但不知道如何按聚合框架上的多个排序定义进行排序。
I have a Collection Students which Stores registered students data in MongoDB. It has StudentName, SaveDate etc. fields. I am displaying the details to my website from my .net framework WebAPI.
What I want to do is Get StudentDetails with prev next student ID's by the order of SaveDate.
My current query gets Student details by student id from below query,
var collection = StudentDB.Collection<Student>("StudyMaterials");
var curfilter = Builders<Student>.Filter.Eq(x => x.studentID, studentID);
studentDetails= collection.Find(curfilter).FirstOrDefault();
This is my Student.cs class,
public class Student
{
[BsonRepresentation(BsonType.String)]
public Guid StudentID {get; set;}
public string StudentName {get; set;}
[BsonRepresentation(BsonType.String)]
public Guid NextStudentID {get; set;}
[BsonRepresentation(BsonType.String)]
public Guid PrevStudentID {get; set;}
}
I want to use aggregation but don't know how to sort by multiple sort definitions on the aggregation framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为你没有必要这样留住学生。我建议如下:
您需要确保文档按
SaveDate
排序。例如,当将新学生插入数据库时,您可以这样做:现在,您可以使用
Skip
和Limit
获取文档:Increment
skip 获取下一个,依此类推。
First of all, I don't think you need to keep the students like that. I suggest the following:
You need to make sure that the documents are sorted by
SaveDate
. For instance, when inserting new students into the database you can do it like this:Now, you can get the documents with
Skip
andLimit
:Increment
skip
to get next and so on.