简体   繁体   English

Mongoose NodeJS-查找每个用户最后插入的记录

[英]Mongoose NodeJS - Find the last inserted record per user

I'm kind of a newbie when it comes to NoSQL and Mongoose. 在NoSQL和Mongoose方面,我是个新手。 I've been searching a bit but couldn't really find what i'm looking for (it makes it even harder that I don't really know the answer). 我一直在搜索,但无法真正找到我想要的东西(这使我更不知道答案的难度更大)。

Basically, I have a list of records that are being added by users. 基本上,我有一个由用户添加的记录列表。 These records contain some data, and I would like to have a list of the latest inserted message of every user. 这些记录包含一些数据,我想列出每个用户的最新插入消息列表。

In SQL I would write; 在SQL中,我会写;

SELECT * FROM records ORDER BY inserted_date GROUP BY user_id;

This would return a set like so; 这将返回一个这样的集合;

[
    {user: 2, insert_date: 2015-08-12T15:15:00, message: "Hi"},
    {user: 3, insert_date: 2015-08-12T15:12:00, message: "Also hi"},
    {user: 5, insert_date: 2015-08-12T15:14:00, message: "Not hi"}
]

I have been looking around, found some answers but those didn't seem to work. 我一直在环顾四周,找到了一些答案,但这些似乎没有用。

I've tried; 我试过了;

https://stackoverflow.com/a/7570091/1493455 https://stackoverflow.com/a/7570091/1493455

And some other aggregate thingie like so; 还有其他一些聚合的东西,就像这样;

Record.aggregate(
    {},
    { 
        $group: {
            _id: '$user_id',
            inserted: { $last: "$inserted" },
            message: { $last: "$message" }
        }
    },
    function(err, messages) {
        console.log(err, messages);
    }
);

which i've copied from another answer - but I don't really understand what its doing and what its even supposed to do. 我已经从另一个答案中复制了该内容-但我并不十分了解它在做什么,甚至应该做什么。

I could ofcourse use a sort and limit query for every user, but this seems very inefficient. 我当然可以为每个用户使用排序和限制查询,但这似乎效率很低。

I'd be pleased if someone can point me in the right direction :) 如果有人可以指出正确的方向,我会很高兴的:)

Yep, duplicate of: Mongoose - find last message from each user 是的,重复的: 猫鼬-查找每个用户的最后一条消息

I ended up fixing my code with; 我最终用修改了我的代码;

Record.aggregate(
    [
        // Matching pipeline, similar to find
        {
            "$match": {}
        },
        // Sorting pipeline
        {
            "$sort": {
                "inserted": -1
            }
        },
        // Grouping pipeline
        {
            "$group": {
                "_id": "$user_id",
                "message": {
                    "$first": "$message"
                },
                "inserted": {
                    "$first": "$inserted"
                }
            }
        },
        // Project pipeline, similar to select
        {
            "$project": {
                "_id": 0,
                "user_id": "$_id",
                "message": 1,
                "inserted": 1
            }
        }
    ],
    function(err, messages) {
        console.log(err, messages);
    }
);

And updating to the latest mongodb (3.4) instead of the old (2.x) I was running (this gives errors because the aggregate is not supported yet). 并更新到最新的mongodb(3.4)而不是我正在运行的旧版本(2.x)(这会产生错误,因为还不支持聚合)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM