mongodb聚合框架组+;项目
我有以下问题: 此查询返回我想要的1个结果:mongodb聚合框架组+;项目,mongodb,mongodb-query,Mongodb,Mongodb Query,我有以下问题: 此查询返回我想要的1个结果: > db.items.aggregate([ {$group: { "_id": "$id", version: { $max: "$version" } } }]) { "result" : [ { "_id" : "b91e51e9-6317-4030-a9a6-e7f71d0f2161", "version" : 1.2000000000000002 } ], "ok" : 1 } 这个
> db.items.aggregate([ {$group: { "_id": "$id", version: { $max: "$version" } } }])
{
"result" : [
{
"_id" : "b91e51e9-6317-4030-a9a6-e7f71d0f2161",
"version" : 1.2000000000000002
}
],
"ok" : 1
}
这个查询(我刚刚添加了投影,以便以后可以查询整个文档)返回多个结果。我做错了什么
> db.items.aggregate([ {$group: { "_id": "$id", version: { $max: "$version" } }, $project: { _id : 1 } }])
{
"result" : [
{
"_id" : ObjectId("5139310a3899d457ee000003")
},
{
"_id" : ObjectId("513931053899d457ee000002")
},
{
"_id" : ObjectId("513930fd3899d457ee000001")
}
],
"ok" : 1
}
找到了答案
1.首先我需要弄到所有的身份证
2.然后我需要查询文档
看起来是这样的:
db.items.find({ _id: { '$in': [ '1', '2', '3' ] } });
(我知道已经很晚了,但仍然在回答,这样其他人就不必去其他地方寻找正确的答案了)
请注意,这将完成您的工作。并非所有蓄能器都在
$project
阶段可用。我们需要考虑在累加器方面我们能做什么,以及我们可以在小组中做些什么。让我们来看看这个:
db.companies.aggregate([{
$match: {
funding_rounds: {
$ne: []
}
}
}, {
$unwind: "$funding_rounds"
}, {
$sort: {
"funding_rounds.funded_year": 1,
"funding_rounds.funded_month": 1,
"funding_rounds.funded_day": 1
}
}, {
$group: {
_id: {
company: "$name"
},
funding: {
$push: {
amount: "$funding_rounds.raised_amount",
year: "$funding_rounds.funded_year"
}
}
}
}, ]).pretty()
在这里,我们检查是否有funding\u轮
为空。然后它将展开
-ed到$sort
和更高的阶段。我们将为每家公司的funding\u rounds
数组的每个元素看到一个文档。因此,我们在这里要做的第一件事是基于以下内容进行$sort
:
funding\u rounds.funded\u year
funding\u rounds.funded\u month
funding\u rounds.funded\u day
$push
构建阵列$push
应该是一个文档的一部分,该文档被指定为组阶段中我们命名的字段的值。我们可以推送任何有效的表达式。在本例中,我们将文档推送到这个数组中,对于我们推送到的每个文档,它都被添加到我们正在积累的数组的末尾。在这种情况下,我们正在推进从募集金额
和资金年
构建的文档。因此,$group
阶段是一个具有\u id
的文档流,我们在其中指定公司名称
请注意,$push
在$group
阶段中可用,但在$project
阶段中不可用。这是因为$group
阶段被设计为获取一系列文档并基于该文档流累积值
$project
另一方面,一次只能处理一个文档。因此,我们可以计算项目阶段中单个文档中数组的平均值。但是这样做的时候,我们每次都会看到一个文档,对于每个文档,它都会经过一个小组阶段,推动一个新的价值,这是$project
阶段不打算做的事情。对于这种类型的操作,我们希望使用$group
让我们看另一个例子:
db.companies.aggregate([{
$match: {
funding_rounds: {
$exists: true,
$ne: []
}
}
}, {
$unwind: "$funding_rounds"
}, {
$sort: {
"funding_rounds.funded_year": 1,
"funding_rounds.funded_month": 1,
"funding_rounds.funded_day": 1
}
}, {
$group: {
_id: {
company: "$name"
},
first_round: {
$first: "$funding_rounds"
},
last_round: {
$last: "$funding_rounds"
},
num_rounds: {
$sum: 1
},
total_raised: {
$sum: "$funding_rounds.raised_amount"
}
}
}, {
$project: {
_id: 0,
company: "$_id.company",
first_round: {
amount: "$first_round.raised_amount",
article: "$first_round.source_url",
year: "$first_round.funded_year"
},
last_round: {
amount: "$last_round.raised_amount",
article: "$last_round.source_url",
year: "$last_round.funded_year"
},
num_rounds: 1,
total_raised: 1,
}
}, {
$sort: {
total_raised: -1
}
}]).pretty()
在$group
阶段,我们使用$first
和$last
累加器。好的,我们可以再次看到,对于$push
,我们不能在项目阶段使用$first
和$last
。因为同样,项目阶段的设计不是基于多个文档累积值。相反,它们被设计为一次一个地重塑文档。使用$sum
运算符计算总轮数。值1仅计算通过该组的文档数,以及与给定\u id
值匹配或分组的每个文档数。这个项目可能看起来很复杂,但它只是让输出变得漂亮而已。只是它包含了上一个文档中提出的num\u轮数
和total\u
db.companies.aggregate([{
$match: {
funding_rounds: {
$ne: []
}
}
}, {
$unwind: "$funding_rounds"
}, {
$sort: {
"funding_rounds.funded_year": 1,
"funding_rounds.funded_month": 1,
"funding_rounds.funded_day": 1
}
}, {
$group: {
_id: {
company: "$name"
},
funding: {
$push: {
amount: "$funding_rounds.raised_amount",
year: "$funding_rounds.funded_year"
}
}
}
}, ]).pretty()
db.companies.aggregate([{
$match: {
funding_rounds: {
$exists: true,
$ne: []
}
}
}, {
$unwind: "$funding_rounds"
}, {
$sort: {
"funding_rounds.funded_year": 1,
"funding_rounds.funded_month": 1,
"funding_rounds.funded_day": 1
}
}, {
$group: {
_id: {
company: "$name"
},
first_round: {
$first: "$funding_rounds"
},
last_round: {
$last: "$funding_rounds"
},
num_rounds: {
$sum: 1
},
total_raised: {
$sum: "$funding_rounds.raised_amount"
}
}
}, {
$project: {
_id: 0,
company: "$_id.company",
first_round: {
amount: "$first_round.raised_amount",
article: "$first_round.source_url",
year: "$first_round.funded_year"
},
last_round: {
amount: "$last_round.raised_amount",
article: "$last_round.source_url",
year: "$last_round.funded_year"
},
num_rounds: 1,
total_raised: 1,
}
}, {
$sort: {
total_raised: -1
}
}]).pretty()