如何检索 Azure 移动服务表的行位置

How retrieve row position for azure mobile services tables

本文关键字:服务 位置 移动 Azure 何检索 检索      更新时间:2023-09-26

我使用 Azure 移动服务(在 HTML 客户端中)用于简单的分数墙应用程序。

首先,应用程序获取前 100 名分数

client.getTable("ScoreWall").orderBy("score").take(100).read().then(...

并稍后显示当前用户位置

client.getTable("ScoreWall").where({"userId":uid}).select("userId","score").read().then(...

但我想检索当前用户在世界排名中的位置。我没有找到任何选择的文档,例如:

select("row_position","userId","score")

如何检索此数据?

想不出在查询中完成所有这些操作的好方法,但给定只有 100 的数据集,进行一些后处理没有任何重大影响。这是我煮熟的东西(我把它放在 js 后端,因为我有一个可以尝试)。

function returnAll(req, res) {
    var todoTable = req.service.tables.getTable('todoitem');
    var n = 1;
    var q = todoTable.select(function () {
        var obj = { num: n, text: this.text };
        n++;
        return obj;
    }).orderBy("text").take(100);
    q.read({
        success: function (results) {
            var Sarah = {};
            results.forEach(function (item) { if (item.text === "Sarah") { Sarah = item } });
            res.send(200, { "results": results, "sarah": Sarah });
        },
        error: function (err) {
            res.send(500, err);
        }
    });
}

显然,这是一个愚蠢的演示,但它明白了重点。结果如下:

{
    "results": [
        {
            "num": 1,
            "text": "Angela"
        },
        {
            "num": 2,
            "text": "Chris"
        },
        {
            "num": 3,
            "text": "John"
        },
        {
            "num": 4,
            "text": "Nikesh"
        },
        {
            "num": 5,
            "text": "Sarah"
        }
    ],
    "sarah": {
        "num": 5,
        "text": "Sarah"
    }
}

在我的示例中,如果您要将其扩展到数百万行,则有几件事需要关注(但我可能会使用单独的作业来计算前 100 名并将其转储到表中或定期用 rank 标记用户对象以解决该规模),但对于 100, 这是体面的。