【发布时间】:2019-04-12 07:16:05
【问题描述】:
我想知道如何才能达到特定的结果。
开始 我正在使用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
如果您确实需要更多解释,请随时询问,我觉得我什至不知道从哪里开始。
非常感谢所有帮助
【问题讨论】:
标签: mongodb laravel laravel-5 aggregation-framework jenssegers-mongodb