获取Parse.com JS查询中每个字段的最新记录 [英] Get latest record per field in Parse.com JS query

查看:91
本文介绍了获取Parse.com JS查询中每个字段的最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Parse数据库中有一个表,其中包含validFrom和uniqueId列。可以有多个具有相同uniqueId的记录(如名称)

I have a table in my Parse database with columns validFrom and uniqueId. There can be multiple records with the same uniqueId (like a name)

对于给定的uniqueIds集合,我必须使用哪个查询来获取具有最新validFrom的项目?我尝试了以下但是这限制了我的搜索到整个集合的1项而不是每个unique_id记录1项:

What query do I have to use to get the items with the latest validFrom for a given set of uniqueIds? I tried the following but this limits my search to 1 item for the entire set rather than 1 item per unique_id record:

  var UpdateObject = Parse.Object.extend("UpdateObject");
  var query = new Parse.Query(UpdateObject);
  query.containedIn("unique_id", uniqueIdsArray).select('status', 'unique_id').descending("validFrom").limit(1);


推荐答案

查询语义是有限的,所以唯一的方法是查询超集并将结果操作到您需要的内容。这在服务器上做得更好,以限制额外对象的传输。

The query semantics are limited, so the only approach is to query for a superset and manipulate the result to what you need. This is better done on the server to limit the transmission of extra objects.

大警告:这是用铅笔和纸做的,而不是运行的parse.app,所以它可能是错的。但最重要的是获取所有uniqueId的所有匹配对象,按uniqueId对它们进行分组,然后为每个组返回一个具有最大validFrom日期的对象...

Big caveat: did this with pencil and paper, not a running parse.app, so it may be wrong. But the big idea is to get all of the matching objects for all of the uniqueIds, group them by uniqueId, and then for each group return the one with the maximum validFrom date...

function updateObjectsInSet(uniqueIdsArray ) {
    var query = new Parse.Query("UpdateObject");
    // find all of the UpdateObjects with the given ids
    query.containedIn("unique_id", uniqueIdsArray);
    query.limit(1000);
    return query.find().then(function(allUpdates) {
        // group the result by id
        var byId = _.groupBy(allUpdates, function(update) { return update.get("unique_id"); });
        // for each group, select the newest validFrom date
        return _.map(byId, function (updates) {  
            _.max(updates, function(update) { return -update.get("validFrom").getTime(); });
        });
    });
}

要将其放入云中,只需将其包裹起来:

To place this in the cloud, just wrap it:

Parse.Cloud.define("updateObjectsInSet", function(request, response) {
    updateObjectsInSet(request.params.uniqueIdsArray).then(function(result) {
        response.success(result);
    }, function(error) {
        response.error(error);
    });
});

然后使用 Parse.Cloud.run()从客户端调用它。

Then use Parse.Cloud.run() from the client to call it.

这篇关于获取Parse.com JS查询中每个字段的最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆