如何获取mongoDB集合的排名位置?

2024-02-25

我有一个 mongoDB 集合,如下所示:

{
    "_id": 1,
    "name": "John Doe",
    "company": "Acme",
    "email": "[email protected] /cdn-cgi/l/email-protection",
    "matches": [171844, 169729, 173168, 174310, 168752, 174972, 172959, 169546]
}
{
    "_id": 2,
    "name": "Bruce Wayne",
    "company": "Wayne Enterprises",
    "email": "[email protected] /cdn-cgi/l/email-protection",
    "matches": [171844, 232333, 233312, 123456]
}
{
    "_id": 3,
    "name": "Tony Stark",
    "company": "Stark Industries",
    "email": "[email protected] /cdn-cgi/l/email-protection",
    "matches": [173844, 155729, 133168, 199310, 132752, 139972]
}
{
    "_id": 4,
    "name": "Clark Kent",
    "company": "Daily Planet",
    "email": "[email protected] /cdn-cgi/l/email-protection",
    "matches": [169729, 174310, 168752]
}
{
    "_id": 5,
    "name": "Lois Lane",
    "company": "Daily Planet",
    "email": "lois.lan[email protected] /cdn-cgi/l/email-protection",
    "matches": [172959, 169546]
}

我需要获取经过筛选的用户列表,但有一个键可以根据用户拥有的“匹配”记录数显示用户的“排名”位置。 应该有一个global ranking位置和一个company ranking位置。

期望的结果应该是这样的(例如过滤 company='Daily Planet'):

{
    _id: 4,
    name: 'Clark Kent',
    company: 'Daily Planet',
    email: '[email protected] /cdn-cgi/l/email-protection',
    points: 3,     // <=
    globalRank: 4, // <=
    companyRank: 1 // <=
},
{
    _id: 5,
    name: 'Lois Lane',
    company: 'Daily Planet',
    email: '[email protected] /cdn-cgi/l/email-protection',
    points: 2,     // <=
    globalRank: 4, // <=
    companyRank: 2 // <=
}

请注意,Clark Kent 在全球排名中排名第 4,因为他有 3 场比赛(John Doe、Bruce Wayne 和 Tony Stark 的比赛比他多),并且在公司排名中排名第一,因为他比任何 Daily Planet 用户的比赛都多。 。

然而,经过几天的研究,我还是找不到办法。 (我什至不知道如何仅进行全球排名或公司排名)。

关于如何解决这个问题,或者如何以不同的方式解决这个问题有什么想法吗?


基本思想是首先对点进行排序points正如你所做的那样,跟进$push将它们放入一个数组中。这确保了元素按排序顺序插入。然后我们$unwind https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/使用includeArrayIndex属性来生成排序数组中与排名相对应的元素的索引。

使用上述逻辑的管道如下(尝试逐步进行以更好地理解):-

aggregate([
    {
        $project: {
            _id: 1,
            name: "$name",
            company: "$company",
            email: "$email",
            points: {
                $size: "$matches"
            }
        }
    }, {
        $sort: {
            points: -1
        }
    },

    {
        $group: {
            _id: {},
            arr: {
                $push: {
                    name: '$name',
                    company: '$company',
                    email: '$email',
                    points: '$points'
                }
            }
        }
    }, {
        $unwind: {
            path: '$arr',
            includeArrayIndex: 'globalRank',
        }
    }, {
        $sort: {
            'arr.company': 1,
            'arr.points': -1
        }
    }, {
        $group: {
            _id: '$arr.company',
            arr: {
                $push: {
                    name: '$arr.name',
                    company: '$arr.company',
                    email: '$arr.email',
                    points: '$arr.points',
                    globalRank: '$globalRank'
                }
            }
        }
    }, {
        $unwind: {
            path: '$arr',
            includeArrayIndex: 'companyRank',
        }
    }, {
        $project: {
            _id: 0,
            name: '$arr.name',
            company: '$arr.company',
            email: '$arr.email',
            points: '$arr.points',
            globalRank: '$arr.globalRank',
            companyRank: '$companyRank'
        }
    }

]);

查询的输出是

/* 1 */
{
    "companyRank" : NumberLong(0),
    "name" : "Bruce Wayne",
    "company" : "Wayne Enterprises",
    "email" : "[email protected] /cdn-cgi/l/email-protection",
    "points" : 4,
    "globalRank" : NumberLong(2)
}

/* 2 */
{
    "companyRank" : NumberLong(0),
    "name" : "Tony Stark",
    "company" : "Stark Industries",
    "email" : "[email protected] /cdn-cgi/l/email-protection",
    "points" : 6,
    "globalRank" : NumberLong(1)
}

/* 3 */
{
    "companyRank" : NumberLong(0),
    "name" : "Clark Kent",
    "company" : "Daily Planet",
    "email" : "[email protected] /cdn-cgi/l/email-protection",
    "points" : 3,
    "globalRank" : NumberLong(3)
}

/* 4 */
{
    "companyRank" : NumberLong(1),
    "name" : "Lois Lane",
    "company" : "Daily Planet",
    "email" : "[email protected] /cdn-cgi/l/email-protection",
    "points" : 2,
    "globalRank" : NumberLong(4)
}

/* 5 */
{
    "companyRank" : NumberLong(0),
    "name" : "John Doe",
    "company" : "Acme",
    "email" : "[email protected] /cdn-cgi/l/email-protection",
    "points" : 8,
    "globalRank" : NumberLong(0)
}

这里的排名是从 0 开始的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取mongoDB集合的排名位置? 的相关文章

  • 是否可以在 MongoDB 中的 $cond 中编写正则表达式

    我需要使用 cond 来组合不同的列 我需要编写一个 cond 如下 create widget sum cond and eq Method POST Url regex widgets 1 0 而且这段代码不对 看来正则表达式不能放在这
  • 如何在MongoDb中保存Timestamp类型值 |爪哇

    从 Java 驱动程序中 我想在 MongoDb 中保存一个类似于下面 json 的文档 ts Timestamp 1421006159 4 我尝试过的选项 选项1 映射 doc new HashMap 1 doc put ts new B
  • 如何指定 GridFS 存储桶?

    这是我的 express js 代码 用于将文件上传和下载到 GridFS var fs require fs var gridStream require gridfs stream var mongoose require mongoo
  • 使用 Express/Node 和 MongoDB 响应 POST 请求

    我正在编写一个程序 该程序使用 React 作为前端 并使用 Express Node API 作为后端 然后在 MongoDB 数据库中执行 CRUD 操作 现在 我正在使用本机 JS fetch API 在前端执行 GET POST 操
  • 在mongodb中编辑子文档N-N关系

    我有一个应用程序 其中article可以链接到多个平台 文章包含平台列表 平台也包含文章列表 有关更多详细信息 请查看我几个月前提出的 stackoverflow 问题 https stackoverflow com a 40377383
  • 使用在 Linux 上运行的 .NET Core 时连接到 MongoDB 失败

    我正在使用 ASP NET Core 2 0 构建一个网站 一个月前刚刚从 1 1 升级 MongoDB 也由 Mongo Atlas 在 AWS 上托管 这个 MongoDB 实例有 3 个副本集 需要 SSL 进行连接 并且已经设置可以
  • mongodb c# 选择特定字段

    需要一些帮助来创建generic按名称选择字段的方法 像这样的东西 T GetDocField
  • 从 MongoDB+Node.js 获取数据到客户端 JavaScript

    如何使用 Node js 连接 MongoDB 然后将结果传递给客户端 JavaScript 并以 HTML 形式显示 var http require http var URL require url var Db require mon
  • 引用 MongoDB Aggregation Pipeline 中的整个文档

    我可以使用 运算符引用 MongoDB 聚合管道中属性的各个值 但是 我如何访问 引用 整个文档 UPDATE 提供一个示例来解释场景 这是我正在尝试做的事情的一个例子 我有一系列推文 每条推文都有一个成员 集群 它指示特定推文属于哪个集群
  • Mongoose 使用 GeoJSON 点作为查询参数调用 geoNear 不起作用

    给定一个为包含 GeoJSON 位置的文档定义的模式 var BranchSchema new Schema location type type String required true enum Point LineString Pol
  • 使用 Morphia 配置 Spring Boot?

    我不想利用 Spring DATA MongoDB 支持 我想利用名为 Morphia 的 MongoDB ORM https github com mongodb morphia https github com mongodb morp
  • MongoDB C# 驱动程序“找不到光标”

    我有一个相当密集的操作MongoCursor循环运行几个小时 在通过 c 驱动程序运行的 vb net 应用程序上 我不太确定是什么导致了它 但一段时间后我遇到了异常 Cursor not found 这可能是由于游标超时造成的 有什么办法
  • 将字符串数组转换为对象 Id 数组

    我有一个字符串数组 let stringObjectIdArray fssdlfsd343 43434234242 342424242 我想使用 mongoose 类型将字符串数组更改为对象 Id 数组 但它不起作用 它仅适用于字符串而不是
  • 显示来自 mongodb 的所有数据并在 doT.js 模板引擎中渲染它

    我想从 mongodb 中提取数据并将其传递给视图 一切似乎都正常 但我没有看到所有 10000 条记录都显示出来 而是只看到了一条 我觉得我非常接近解决它 但我陷入困境 我正在使用node mongodb native express和d
  • mongodb - 检索数组子集

    看似简单的任务对我来说是一个挑战 我有以下 mongodb 结构 services TCP80 data status 1 delay 3 87 ts 1308056460 status 1 delay 2 83 ts 1308058080
  • 使用mongoid动态创建索引

    我有一项为我的文档创建新字段的工作 我想在这项工作结束时创建该字段的索引 我试过 Model index field gt 1 and also Mongoid Sessions default rating prediction ensu
  • 如何使用 node.js / mongodb 在 HTML 中显示任意、无模式数据

    我使用 mongodb 将应用程序错误日志存储为 json 文档 我希望能够将错误日志格式化为 HTML 而不是将纯 json 返回到浏览器 日志是完全无模式的 它们可以随时更改 因此尝试执行此操作 在 Jade 中 是没有用的 var i
  • 猫鼬的深层填充

    我有两个模式 一张用于用户 另一张用于帖子 在用户模式中 我有latestPost的一个属性 它是帖子模式中条目的ObjectId 当我加载用户对象时 我想将 lastestPost 作为对象获取 其中包含用户架构中作者的用户名 其中作者是
  • 如何在 Mongoose 中执行查找查询?

    我在 mongodb 中有一组电子书数据 例如 id ObjectId 58b56fe19585b10cd42981d8 cover path D Ebooks uploads ebooks cover 1488285665748 img1
  • 更新插入 MongoDB 时如何防止出现“_t”字段?

    我有一个应用程序 它使用 MongoDB 的 C 驱动程序将 Upsert 插入 MongoDB 数据库 当我打电话给Update函数 我无法指定我要更新的类型 然后 t字段插入元素的类型 这是我用来更新插入的代码 collection U

随机推荐