组合/聚合查询与解析云代码 [英] Group By / Sum Aggregate Query with Parse Cloud Code

查看:768
本文介绍了组合/聚合查询与解析云代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Parse数据库中有Inventory表,有两个相关字段:productId和quantity。收到货件时,将创建包含productId和数量的记录。类似地,当销售发生时,使用productId和数量(其将是负的,因为在销售之后库存将减少)进行库存记录。



我想使用Parse Cloud Code在Inventory表上运行group by / sum聚合查询,输出包含唯一productIds作为键的字典,



我已经看到一些老帖子说Parse不这样做,但更新的帖子指的是云代码作为cloud代码指南中的averageStart: https://parse.com/docs/cloud_code_guide



然而,似乎Parse.Query在averageStars中使用的最大限制为1000条记录。因此,当我总数量列,我只这样做1000记录,而不是整个表。有没有一种方法,我可以通过/ sum在清单表中的所有记录计算组?








库存表



productId数量 / p>

记录1:AAAAA 50



记录2:BBBBB 40



记录3:AAAAA -5



记录4:BBBBB -2



记录5:AAAAA 10



记录6:AAAAA -7



输出字典:
{AAAAA: 48,BBBBB:38}

解决方案

您可以使用 Parse.Query.each )。没有限制。如果您的课程太多,则会超时。



请参阅 docs



例如:

  var totalQuantity = 0; 
var inventoryQuery = new Parse.Query(Inventory);
inventoryQuery.each(
function(result){
totalQuantity + = result.get(quantity);
},{
success:function(){
//循环通过一切
},
错误:function(error){
//错误是Parse.Error的一个实例
}
});
});

如果超时,您必须构建类似 this


I have Inventory table in my Parse database with two relevant fields: productId and quantity. When a shipment is received, a record is created containing the productId and quantity. Similarly, when a sale occurs, an inventory record is made with the productId and quantity (which will be negative, since the inventory will decrease after the sale).

I would like to run a group by/ sum aggregate query on the Inventory table with Parse Cloud Code that outputs a dictionary containing unique productIds as the keys and the sum of the quantity column for those Ids as the values.

I have seen a number of old posts saying that Parse does not do this, but then more recent posts refer to Cloud Code such as averageStart in the Cloud Code Guide: https://parse.com/docs/cloud_code_guide

However, it seems that Parse.Query used in averageStars has a maximum limit of 1000 records. Thus, when I sum the quantity column, I am only doing so on 1000 records rather than the whole table. Is there a way that I can compute the group by/ sum across all the records in the inventory table?


For example:

Inventory Table

productId quantity

Record 1: AAAAA 50

Record 2: BBBBB 40

Record 3: AAAAA -5

Record 4: BBBBB -2

Record 5: AAAAA 10

Record 6: AAAAA -7

Output dictionary: {AAAAA: 48, BBBBB: 38}

解决方案

You can use Parse.Query.each(). It has no limit. If your class has too many entries it will timeout though.

See docs

e.g.:

var totalQuantity = 0;
var inventoryQuery = new Parse.Query("Inventory");
inventoryQuery.each(
    function(result){
        totalQuantity += result.get("quantity");
    }, {
        success: function() {
            // looped through everything
        },
        error: function(error) {
            // error is an instance of Parse.Error.
        }
    });
});

If it times out, you have to build something like this.

这篇关于组合/聚合查询与解析云代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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