Open fernando2amigos opened 2 years ago
Just as a proof of concept, I've altered the order the pipeline is built in Builder.php and I got the expected results.
Hey guys, I wanted to make a change and contribute but beside cloning the repo and building the containers I couldn't make the "tests" container run nor install dependencies. I know this isn't the correct place to discuss this, but the CONTRIBUTING file hasn't any technical directions onto this matter. If you could give some help I'd be glad to drop my two cents.
I've run into the same problem like you. However, I find a solution by using raw() function. Here is an example:
$cursor = DB::connection('mongodb')->table('documents')->raw(function ($collection) use ($type, $by) { return $collection->aggregate([ [ '$match' => [ '$and' => [ ['deleted_at' => null], ], ], ], [ '$group' => [ '_id' => [ '$dateToString' => [ 'format' => '%Y-%m', 'date' => '$created_at', ], ], 'count' => ['$sum' => 1], ], ], [ '$sort' => [ 'count' => -1, ], ], [ '$limit' => 12, ], ]); }); }
You can also use DB::connection('mongodb')->raw() to get a mongodb connection directly.
Hope this helps.
PS: I don't think changing the original code would be a good idea, because in most situation, the original lib works just fine, and what we need here is really rare.
Don't know how to get the code block working correctly, sorry.
Hey, I'm not that anymore in that project, but yes I ended up building methods that constructed the array pipelines and using a ton of raw() queries.
Regarding the code block, you should use three back-ticks to open a whole block and not just formatting inline (backtick)(backtick)(backtick)php ... (backtick)(backtick)(backtick)
This is
and indented {
[piece of => code]
}
Description:
The order of stages in the resulting "aggregate" pipeline alters the final result
Steps to reproduce
Make any Laravel model an store the sample document in any collection Dump the executed queries. (
dump(DB::connection('mongodb')->getQueryLog());
) Observe difference between the right query returning results and the one generated by the packageExpected behaviour
To not assume the "project" to be the last stage in the pipeline
Actual behaviour
It's setting the "project" stage as the last stage in the aggregation pipeline
This is a sample document in my collection:
What I need to do is to get the SUM of the items in each "visitors" array of every matching document, so if the only matching document would be this one, then the result of the aggregation would be
4p
.I've manually written this query which yields the expected results:
And this is the one written by the package: (not the swapped places between
$group
and$project
)This is the Model's code:
Maybe I'm missing something. In that case, let me know and please add it to documentation.
Thanks in advance!