- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我最近开始在 Mongodb 中为 POC 工作。我在下面有一个 json 集合
db.ccpsample.insertMany([
{
"ccp_id":1,
"period":601,
"sales":100.00
},
{
"ccp_id":1,
"period":602,
"growth":2.0,
"sales":"NULL" ##sales=100.00*(1+(2.0/100)) -- 100.00 comes from(ccp_id:1 and period=601)
},
{
"ccp_id":1,
"period":603,
"growth":3.0,
"sales":"NULL" ##sales=100.00*(1+(2.0/100))**(1+(3.0/100))-- 100.00 comes from(ccp_id:1 and period=601) 2.0 comes from (ccp_id:2 and period=602)
},
{
"ccp_id":2,
"period":601,
"sales":200.00
},
{
"ccp_id":2,
"period":602,
"growth":2.0,
"sales":"NULL" ##sales=200.00*(1+(2.0/100))
},
{
"ccp_id":2,
"period":603,
"growth":3.0,
"sales":"NULL" ##same like above
}
])
并且我需要使用上述文档计算具有 NULL 的销售字段,匹配条件的 ccp_id 应该相同,期间字段应该等于 601。我添加了一行来演示上面集合本身中销售字段的计算。我尝试使用 $graphlookup 但没有运气。你们可以帮助或建议一些方法吗?
最佳答案
您可以使用以下聚合:
db.ccpsample.aggregate([
{ $sort: { ccp_id: 1, period: 1 } },
{
$group: {
_id: "$ccp_id",
items: { $push: "$$ROOT" },
baseSale: { $first: "$sales" },
growths: { $push: "$growth" }
}
},
{
$unwind: {
path: "$items",
includeArrayIndex: "index"
}
},
{
$project: {
cpp_id: "$items.cpp_id",
period: "$items.period",
growth: "$items.growth",
sales: {
$cond: {
if: { $ne: [ "$items.sales", "NULL" ] },
then: "$items.sales",
else: {
$reduce: {
input: { $slice: [ "$growths", "$index" ] },
initialValue: "$baseSale",
in: { $multiply: [ "$$value", { $add: [1, { $divide: [ "$$this", 100 ] }] } ] }
}
}
}
}
}
}
])
基本上要计算 n-th
元素的值,您必须知道以下几点:
$group
中的$first
)增长
的数组($group
中的$push
)n
表示你必须执行多少次乘法要计算索引,您应该将所有元素$push
放入一个数组中,然后使用$unwind
使用 includeArrayIndex
选项将展开数组的索引插入到字段 index
。
最后一步计算累积乘法。它使用 $slice使用 index
字段来评估应该处理多少 growths
。所以601
会有一个元素,602
会有两个元素,以此类推。
那么该是 $reduce 的时间了处理该数组并根据您的公式执行乘法: (1 + (growth/100)
)
关于Mongodb计算查询--累积乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49430905/
美好的一天! 我正在制作一个出勤检查程序,单击一次时显示橙色按钮,单击两次时显示红色按钮,单击 3 次时显示黑色按钮。我在如何累积 getClickCount() 值方面遇到问题,因为对于按钮要注册
我似乎无法在 Adobe 网站上找到明确的答案。使用 ColdFusion 10,11 甚至 2016,更新(修补程序)是否累积? 例如,ColdFusion 的修补程序高达 hotfix_023
我是随机森林新手,我有一个关于回归的问题。我正在使用 R 包 randomForests 来计算 RF 模型。 我的最终目标是选择对预测连续性状很重要的变量集,因此我正在计算一个模型,然后删除准确度平
目前我们有一个发布/消费者服务,消费者将收到的消息写入 AWS S3。我们目前每月编写超过 100.000.000 个对象。但是,我们可以根据一些规则对这些消息进行分组,以节省一些钱。这些规则可以是这
假设我有一个二叉树: data BinTree a = Nil | Branch a (BinTree a) (BinTree a) 我想在这样的结构上做一个累积映射: mapAccum ::
我正在使用内核估计,我应用了 density函数从 R 到我的数据文件(双变量),经过几次统计处理后,我需要转换这些数据,这就是我的问题: 是否有非参数方法的逆累积分布函数? 我尝试过 Google、
不确定以前是否有人问过这个问题,尝试搜索它但找不到任何相关内容。 我试图获得一个累积的字符串聚合,即仅运行不同值的聚合。这是我正在寻找的结果的示例。 我尝试使用 string_agg 函数,但它仅在用
我想找到累积的 bind.rows。这是我想要实现的小例子。我将使用 dslabs 包中的 gapminder 数据集进行演示。 library(tidyverse) library(dslabs)
在 Linux 中使用 tomcat 进程时,我们观察到时间字段显示5506:34(累积 CPU 时间)。在探索时,这是在进程的整个生命周期中运行所花费的 CPU 时间的百分比。 由于这是一个 Jav
我有一些数据可以使用 pyparsing 中的 OneorMore 函数进行解析。比如, fun = OneorMore( foo.setResultsName("foo") + bar.setRe
我试图弄清楚是否有一种简单的方法可以解决 pandas 的以下问题。假设我有四个容器,A、B、C、D,每个容器都有特定的体积。假设我现在得到了一定量的液体,我想用它来填充这些容器。我怎样才能想出一个“
我正在尝试编写一个函数来检测所有上升沿 - 向量中值超过特定阈值的索引。这里描述了类似的东西:Python rising/falling edge oscilloscope-like trigger
这个问题在这里已经有了答案: Multiplying elements of a column in skipping an element after each iteration (3 个答案)
有没有办法获取数据框中每一行的值计数? |f1|f2| ------- v1 | a value_counts -> {a:1} v2 | a value_counts -> {a:2} v3 |
我目前正在尝试对我正在构建的计算器(使用复合模式)进行测试。第一种方法应该添加 75 美元,效果很好,但是当第二种方法运行时,“服务”被重置并且有0 美元作为工作成本。如果我将这两种方法合二为一,那么
我有一个如下所示的文档: 数据.txt 100, "some text" 101, "more text" 102, "even more text" 我使用正则表达式处理它并返回一个新的处理文档,如
假设我有这个: function getAllPromises(key: string, val: any): Promise { const subDeps = someHash[key]; c
我在 mysql 中有表“cumul_sum”,我想根据条件划分“cumulative”列,即如果此列中的值 >= 70,则这些值应存储在名为“others”的新列中"并且前面应该存放对应的sku_i
我正在做一个用 C++ 刺激 ATM 的项目,但在使用累加器时遇到了一些问题,我的问题是:我正在使用开关(这里是情况 1)来更改在包含的函数中声明的 2 个变量的值switch(),但是值只在情况 1
我希望能够使用 accumulate 对 vector 中的每隔一对元素进行累加。我尝试了以下但没有成功,为非空、非零 vector 返回错误 return std::accumulate(vec.b
我是一名优秀的程序员,十分优秀!