mongodb查询出某个字段最大值

字段进行降序排序,然后取出集合第一条数据

以下为 取出student集合stu_id字段的最大的一条:

db.student.find().sort({stu_id:-1}).skip(0).limit(1);

 

 

在c#代码里的写法:

  //1、计算id最大值,然后将新的id在此基础上加1
                var data = await database.GetCollection<test_info>("test_info").Find(x => x.id > 0).SortByDescending(x => x.id).Skip(0).Limit(1).ToListAsync();

 

posted @ 2021-10-27 09:58  沐雪架构师  阅读(2714)  评论(0编辑  收藏  举报