使用mongo一次获取多个项目的最后一条记录(Get last record for several items at once with mongo)

编程入门 行业动态 更新时间:2024-10-28 06:28:30
使用mongo一次获取多个项目的最后一条记录(Get last record for several items at once with mongo)

在我的mongo数据库中,我基本上有2个集合:

学生们

{_id: ObjectID(539ab7ffefbb93120c9697f7), firstname: 'Arnold', lastname: 'Smith'} {_id: ObjectID(539ab7ffefbb93120c5473c3), firstname: 'Steven', lastname: 'Jens'}

分数

{ date: '2014-06-12', value: 12, pupilID: 539ab7ffefbb93120c9697f7} { date: '2014-06-05', value: 9, pupilID: 539ab7ffefbb93120c9697f7} { date: '2014-05-10', value: 17, pupilID: 539ab7ffefbb93120c9697f7} { date: '2014-05-10', value: 7, pupilID: 539ab7ffefbb93120c5473c3}

有没有办法用mongoshell来获得每个学生的最后一个标记,而不必手动循环学生列表并获得每个学生的最后一个标记?

目前我循环每个学生并执行:

db.marks.find({pupilID: pupilID}).sort({_id: -1}).limit(1)

但是,如果标记集合包含大量项目,我会非常关注这些表现。

In my mongo database, I have basically 2 collections:

pupils

{_id: ObjectID(539ab7ffefbb93120c9697f7), firstname: 'Arnold', lastname: 'Smith'} {_id: ObjectID(539ab7ffefbb93120c5473c3), firstname: 'Steven', lastname: 'Jens'}

marks

{ date: '2014-06-12', value: 12, pupilID: 539ab7ffefbb93120c9697f7} { date: '2014-06-05', value: 9, pupilID: 539ab7ffefbb93120c9697f7} { date: '2014-05-10', value: 17, pupilID: 539ab7ffefbb93120c9697f7} { date: '2014-05-10', value: 7, pupilID: 539ab7ffefbb93120c5473c3}

Is there a way with mongoshell to get the last mark of each pupils without having to manually loop through the list of pupils and get the last mark for each one ?

Currently I loop through each pupils and perform a:

db.marks.find({pupilID: pupilID}).sort({_id: -1}).limit(1)

But I'm quite concerned regarding the performances if the marks collections contains a high number of items.

最满意答案

那么你的日期不是最好的例子,因为它们是字符串。 您应该将它们转换为正确的“日期”类型,但至少它们是排序的词汇。

不是你似乎暗中寻找的“加入”,但你可以从你的“标记”集合中获得每个学生的$last标记,这可能会帮助你的结果:

db.marks.aggregate([
    { "$sort": { "date": 1 } },
    { "$group": {
        "_id": "$pupilID",
        "date": { "$last": "$date" },
        "value": { "$last": "$value" }
    }}
]}
 

这将为每个“pupilID”提供按日期标记的最后一个标记“值”。 数据的加入取决于您,但这比循环整个集合或以其他方式启动查询每个“pupil”更好。

Well your dates are not the best example here as they are strings. You should convert them to proper "Date" types, but at least they are lexical for sorting.

Not the "join" you seem to be implicitly looking for, but you can get the $last mark for each student from your "marks" collection, which will probably do some way to helping your result:

db.marks.aggregate([
    { "$sort": { "date": 1 } },
    { "$group": {
        "_id": "$pupilID",
        "date": { "$last": "$date" },
        "value": { "$last": "$value" }
    }}
]}
 

And that will give you the last mark "value" by date for each "pupilID". The joining of data is up to you, but this is better than looping whole collections or otherwise firing off on query per "pupil".

更多推荐

本文发布于:2023-07-24 01:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   项目   mongo   record   items

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!