gpt4 book ai didi

java - MongoDb 聚合组 Arrays 对象

转载 作者:行者123 更新时间:2023-12-01 17:46:04 26 4
gpt4 key购买 nike

我有一个 mongo 文档,例如:

        {
"id_" : ...,
"company" "AAA":,
"userId" : 80081624,
"features" : [
{
"id" : 1,
"nombre" : "size",
"normal" : 1, (1 or 0)
"alt" : 0, (1 or 0)
},
...
,{
"id" : 8,
"nombre" : "name",
"normal" : 0, (1 or 0)
"alt" : 1, (1 or 0)
}
]
},
{
"id_" : ...,
"company" "BBB":,
"userId" : 8008555,
"features" : [
{
"id" : 1,
"name" : "yyyy",
"normal" : 0, (1 or 0)
"alt" : 1, (1 or 0)
},
{...},{...}
]
}

我希望能够按公司对它们进行分组,并获得它们的“正常”或“替代”百分比。现在我的代码是这样的:

collection.aggregate(
Arrays.asList(
Aggregates.unwind("$inductores"),

Aggregates.group("$features.id",
Accumulators.avg("n","$features.normal"),
Accumulators.avg("alt","$features.alt")),
Aggregates.sort(ascending("_id")),
Aggregates.project(new org.bson.Document("id","$features.id")
.append("n","$n")
.append("alt","$alt"))
)

).forEach(printBlock);

这通过 shell 向我展示:

{"_id": 1, "n": 1.0, "alt": 0.0}
{"_id": 8, "n": 0.7, "alt": 0.0}
{....}

但我想告诉我这个:

    {
"_id" : ObjectId("5e45056845b5da211aa04ec4"),
"company" : "BBB",
"features" : [
{
"id" : 1,
"name" : "yyy",
"normal" : “20%”,
"alt" : “45%”
},
{...}
,{
"id" : 3,
"nombre" : "XXX",
"normal" : “10%”,
"alt" : “20%”
}
]
},
{...}

我首先尝试了多种方法对公司进行分组,但未能获得结果。

<小时/>

编辑:我已经得到了 mongo 语言的命令,我只需要把它翻译成 java,但这对我来说是不可能的。我使用的是驱动程序版本3.12

db.collection.aggregate([
{"$unwind":"$feature"},

{
$group: { "_id": {"company" :"$company", "name":"$feature.name", "Id":"$feature.id"},
"n" : {"$sum":"$feature.normal"},
"a1" : {"$sum":"$feature.alterado1"},
}
},

{$sort: {"_id.Id": 1}},

{
$project: {
"name":"$feature.name",
"normal": $concat: [{ $toString: { "$round": [ { "$multiply": [ { "$divide": [100,{"$sum":["$n","$a"]}]}, "$n"]},2] }}," %"],
"alt": $concat: [{ $toString: { "$round": [ { "$multiply": [ { "$divide": [100,{"$sum":["$n","$a"]}]}, "$a"]},2] }}," %"],
}

},

{ $group : {
_id : "$_id.company",
feature: {
$push: {
id:"$_id.Id",
name:"$_id.name",
Normal:"$normal",
Alterate: "$alt"
}
}
}
},

],{ allowDiskUse: true }

).pretty();

最佳答案

我不熟悉 Java 驱动程序。我可以建议如何在 mongo shell 中执行此操作,然后由您将其转换为 Java。

假设该集合将包含每个公司的多个文档,并且每个公司文档可以具有相同的功能和不同的选项,则管道将需要

  • 展开功能数组,以便每个文档都有一家拥有 1 个功能的公司
  • 按公司特征对文档进行分组,并计算出现次数以及正常/替代的总数。由于模式要求 alt 和 normal 为 1 或 0,因此您可以将它们相加,而不是使用 $cond,因为如果它们是 boolean 值,则需要使用 $cond 。注意:如果可以安全地假设某项功能要么是正常的,要么是替代性的,而绝不会两者兼而有之或两者都不是,那么您只需计算一个,就可以计算替代性的百分比1 -(正常百分比),这可能会快一点。
  • 根据计数值计算百分比
  • 按公司重新分组并将功能插入数组
db.collection.aggregate([
{$unwind: "$features"},
{$group: {
_id:{company:"$company",feature:"$features.id"},
name:{$first:"$name"},
nombre:{$first:"$nombre"},
count:{$sum:1},
normal:{$sum:"$normal"},
alt:{$sum:"$alt"}
}},
{$project:{
normal:{$round:[{$mulitply:[100,{$divide:["$normal","$count"]}]},0]},
alt:{$round:[{$mulitply:[100,{$divide:["$alt","$count"]}]},0]}
}},
{$group:{_id:"$company", features:{$push:"$features"}}}
])

关于java - MongoDb 聚合组 Arrays 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60864071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com