在 MongoDB C# 上获取上一个、当前、下一个

发布于 2025-01-09 06:57:03 字数 870 浏览 0 评论 0原文

我有一个学生集合,它将注册学生数据存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你搞怪i 2025-01-16 06:57:03

首先,我认为你没有必要这样留住学生。我建议如下:

public class Student
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public DateTime SaveDate { get; set; }
}

您需要确保文档按 SaveDate 排序。例如,当将新学生插入数据库时​​,您可以这样做:

var student = new Student
{
    Name = "Sara",
    SaveDate = DateTime.UtcNow
};
context.Students.InsertOne(student);

现在,您可以使用 SkipLimit 获取文档:

var skip = 0;
var limit = 1;
var context = new MongoContext();
var filter = Builders<Student>.Filter.Empty;
var result = context.Students.Find(filter).Skip(skip).Limit(limit).FirstOrDefault();

Increment skip 获取下一个,依此类推。

First of all, I don't think you need to keep the students like that. I suggest the following:

public class Student
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public DateTime SaveDate { get; set; }
}

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:

var student = new Student
{
    Name = "Sara",
    SaveDate = DateTime.UtcNow
};
context.Students.InsertOne(student);

Now, you can get the documents with Skip and Limit:

var skip = 0;
var limit = 1;
var context = new MongoContext();
var filter = Builders<Student>.Filter.Empty;
var result = context.Students.Find(filter).Skip(skip).Limit(limit).FirstOrDefault();

Increment skip to get next and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文