亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MongoDB聚合總和直到條件(if/else)或計數價格和數量問題

MongoDB聚合總和直到條件(if/else)或計數價格和數量問題

一只斗牛犬 2021-06-28 17:23:36
問題我正在寫,可以計算的功能avg price和整個value基礎要求quantity,例如:async funcName (first_100) {        let market_quantity = await auctions_db.aggregate([            {                $match: {                    item: 9999,                }            },            {                $sort: {                    price: 1                }            },            //RESULT        ]);}//結果在這一點上,我有以下文檔:{    item_id: 9999,    price: 1..Number    quantity: 1..200    value: price x quantity //it's not virtual field},{    another doc...}正如我上面提到的,我想要的東西是$sum price字段直到quantity不會達到 100 或 100+(或任何其他數字取決于函數參數)。我不需要$sum前 100 個文檔(按price升序排序,我會.limit在這種情況下使用)所以很明顯我有兩種方法可以做到這一點。至于現在我使用collection.find({condition}).cursor()相同的條件并在單獨的 doc 上迭代 doc for loop,但我知道(實際上一個朋友告訴我)MongoDB可以aggregate通過$cond(if/else) 塊通過stage來做同樣的事情。實際上,我想在這種情況下,下一個$operator之后//Result應該是$group階段,如下所示:            {                $group: {                    _id: "$lastModified",                    quantity: {$sum: {$cond: if/else }" $quantity"},                    if (below 100) {$sum: "$value" }                }            }但我不知道該怎么做。那么誰能給我舉個例子呢?或者aggregate階段.find({}).cursor和手動迭代之間沒有區別?
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

實際上,我通過find({query}).cursor()語法解決了我的問題并推送到數組,例如:


        let market_quantity = await auctions_db.find({query}).cursor();

        for (let order = await market_quantity.next(); order != null; order = await market_quantity.next()) {

            if (quantity < arg_quantity) {

                quantity += order.quantity;

                price_array.push(order.price);

                value += order.buyout;

            } else {

                break;

            }

        }


查看完整回答
反對 回復 2021-07-01
  • 1 回答
  • 0 關注
  • 379 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號