Laravel MongoDB - 聚合、排序查询
Laravel MongoDB - aggregation, ordering query
我想知道我怎样才能达到特定的结果。
开始
我正在使用 https://github.com/jenssegers/laravel-mongodb
下面的代码示例用于获取包含我在奖励节点中的特定 slug 的文档数组。到那时,一切都按预期进行。
$array = [
'rewards.slug' => ['$eq' => 'example_slug'],
'expired' => ['$gte' => \Carbon\Carbon::now()->toDateTimeString()]
];
$models = Master::raw(function ($collection) use (&$array) {
return $collection->find(
$array, ["typeMap" => ['root' => 'array', 'document' => 'array']])
->toArray();
});
我的示例文档
{
"_id": {
"$oid": "5be4464eafad20007245543f"
},
"some_int_value": 100,
"some_string_value": "String",
"rewards": [
{
"slug": "example_slug",
"name": "Example slug",
"quantity": 4,
"estimated": {
"value": 18750
}
},
{
"slug": "example_slug",
"name": "Example slug",
"quantity": 1,
"estimated": {
"value": 100
}
},
{
"slug": "other_example",
"name": "Other slug example",
"quantity": 1,
"estimated": {
"value": 100
}
}
],
"expires": "2018-11-08 20:20:45",
}
想要的结果
我想实现一些更复杂的查询,它会执行以下操作。
- 检索文件:伪
select all documents that contain reward "slug": "example_slug", sum the quantity of them, return greater than X quantity documents, order by sum quantity desc
- 和上面的非常相似
select all documents that contain reward "slug": "example_slug", sum estimated.value, return greater than X estimated.value documents, order by sum of estimated.value desc
如果您确实需要更多解释,请随时询问,我觉得我什至不知道从哪里开始。
非常感谢所有帮助
您可以在 3.6 中使用以下聚合。
$addFields
创建一个额外的 slugcount
字段来保存结果。
$filter
奖励与 slug 匹配 example_slug
然后 $sum
对 quantity
字段求和。
$match
with $gt > X
- 聚合表达式过滤所有匹配数量之和大于X
的文档
$sort
slugcount desc 和 $project
排除以从最终响应中删除 slugcount。
db.colname.aggregate([
{"$addFields":{
"slugcount":
{"$let":{
"vars":{
"mslug":{
"$filter":{
"input":"$rewards",
"cond":{"$eq":["$$this.slug","example_slug"]}
}
}
},
"in":{"$sum":"$$mslug.quantity"}
}}
}},
{"$match":{"slugcount":{"$gt":X}}},
{"$sort":{"slugcount":-1}},
{"$project":{"slugcount":0}}
])
类似于
ModelName::raw(function ($collection) {
return $collection->aggregate([
['$match' => ['expired' => ['$gte' => \Carbon\Carbon::now()->toDateTimeString()]]],
['$addFields' => [
'slugcount'
['$let' => [
'vars' => [
'mslug' => [
'$filter' => [
'input' => '$rewards',
'cond' => ['$eq' => ['$$this.slug','example_slug']]
]
]
],
'in' => ['$sum' => '$$mslug.quantity']
]]
]],
['$match' => ['slugcount'=> ['$gt' => X]]],
['$sort' => ['slugcount' => -1]],
['$project' => ['slugcount' => 0]]]);
});
您可以将 quantity
替换为 estimated.value
以进行第二次聚合。
我想知道我怎样才能达到特定的结果。
开始 我正在使用 https://github.com/jenssegers/laravel-mongodb
下面的代码示例用于获取包含我在奖励节点中的特定 slug 的文档数组。到那时,一切都按预期进行。
$array = [
'rewards.slug' => ['$eq' => 'example_slug'],
'expired' => ['$gte' => \Carbon\Carbon::now()->toDateTimeString()]
];
$models = Master::raw(function ($collection) use (&$array) {
return $collection->find(
$array, ["typeMap" => ['root' => 'array', 'document' => 'array']])
->toArray();
});
我的示例文档
{
"_id": {
"$oid": "5be4464eafad20007245543f"
},
"some_int_value": 100,
"some_string_value": "String",
"rewards": [
{
"slug": "example_slug",
"name": "Example slug",
"quantity": 4,
"estimated": {
"value": 18750
}
},
{
"slug": "example_slug",
"name": "Example slug",
"quantity": 1,
"estimated": {
"value": 100
}
},
{
"slug": "other_example",
"name": "Other slug example",
"quantity": 1,
"estimated": {
"value": 100
}
}
],
"expires": "2018-11-08 20:20:45",
}
想要的结果
我想实现一些更复杂的查询,它会执行以下操作。
- 检索文件:伪
select all documents that contain reward "slug": "example_slug", sum the quantity of them, return greater than X quantity documents, order by sum quantity desc
- 和上面的非常相似
select all documents that contain reward "slug": "example_slug", sum estimated.value, return greater than X estimated.value documents, order by sum of estimated.value desc
如果您确实需要更多解释,请随时询问,我觉得我什至不知道从哪里开始。
非常感谢所有帮助
您可以在 3.6 中使用以下聚合。
$addFields
创建一个额外的 slugcount
字段来保存结果。
$filter
奖励与 slug 匹配 example_slug
然后 $sum
对 quantity
字段求和。
$match
with $gt > X
- 聚合表达式过滤所有匹配数量之和大于X
$sort
slugcount desc 和 $project
排除以从最终响应中删除 slugcount。
db.colname.aggregate([
{"$addFields":{
"slugcount":
{"$let":{
"vars":{
"mslug":{
"$filter":{
"input":"$rewards",
"cond":{"$eq":["$$this.slug","example_slug"]}
}
}
},
"in":{"$sum":"$$mslug.quantity"}
}}
}},
{"$match":{"slugcount":{"$gt":X}}},
{"$sort":{"slugcount":-1}},
{"$project":{"slugcount":0}}
])
类似于
ModelName::raw(function ($collection) {
return $collection->aggregate([
['$match' => ['expired' => ['$gte' => \Carbon\Carbon::now()->toDateTimeString()]]],
['$addFields' => [
'slugcount'
['$let' => [
'vars' => [
'mslug' => [
'$filter' => [
'input' => '$rewards',
'cond' => ['$eq' => ['$$this.slug','example_slug']]
]
]
],
'in' => ['$sum' => '$$mslug.quantity']
]]
]],
['$match' => ['slugcount'=> ['$gt' => X]]],
['$sort' => ['slugcount' => -1]],
['$project' => ['slugcount' => 0]]]);
});
您可以将 quantity
替换为 estimated.value
以进行第二次聚合。