Laravel-mongodb: how to use $lookup ?

Created on 15 May 2016  路  10Comments  路  Source: jenssegers/laravel-mongodb

I want to use $lookup. but i don't know how to use it.
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

Most helpful comment

    return Users::raw(function($collection)
    {
        return $collection->aggregate(
            [[
                '$lookup' => [
                    'as'=>'info',
                    'from'=>'beroep',
                    'foreignField'=>'voornaam',
                    'localField'=>'voornaam'
                ]
            ]]
        );
    });

All 10 comments

I would also really like to know how this is done. I have tried looking in the docs and the source, but it is nowhere to be mentioned. The operators field also does not include the lookup key. When making a whereRaw request like this:

$data = $data->whereRaw(array( '$lookup' => array('as':'field','from':'collection','foreignfield':'somefield','localfield':'somefield') ))

The query builder throws the following error:

MongoCursorException in Builder.php line 310:
localhost:27017: Can't canonicalize query: BadValue unknown top level operator: $lookup

Regards

Ok so I experimented some more with raw queries found here: https://github.com/jenssegers/laravel-mongodb/wiki/Complexe-aggragate-call

I have tried the following things with mixed results:

// Returns error combined with previous queries ($or/$and/$nor entries need to be full objects)
// Looks like the eloquent part is being overwritten which is in line with the documentation
$data = $data->whereRaw([
    [
        '$lookup' => [
            'as'=>'field',
            'from'=>'collection',
            'foreignField'=>'somefield',
            'localField'=>'somefield'
        ]
    ]
]);

// Returns an object containing all sorts of query parameters
$data = $data->aggregate([
    '$lookup' => [
        'foreignField' => 'ifcguid',
        'localField' => 'site.ifcguid',
        'as' => 'site',
        'from' => 'ifcobjects'
    ]
]);

// This works, partly. The sad part is that it overwrites any previous eloquent query inputs like 
// projection or where clauses. An example of that would be $data = $data->where('ifcguid', $guid); 
// Which in our case is supposed to return but one result. Now the whole database is returned with
// the lookup aggregation fullfilled
$results = $data->raw(function($collection)
{
    return $collection->aggregate(
        [
            '$lookup'    => [
                'foreignField' => 'ifcguid',
                'localField' => 'site.ifcguid',
                'as' => 'site',
                'from' => 'ifcobjects'
            ]
        ]
    );
});


// This does not seem to do anything
$data = $data->whereRaw(function($collection)
{
    return $collection->aggregate(
        [
            '$lookup'    => [
                'foreignField' => 'ifcguid',
                'localField' => 'site.ifcguid',
                'as' => 'something',
                'from' => 'ifcobjects'
            ]
        ]
    );
});

Any feedback would be greatly appreciated

Did anyone ever get this solved? I am trying myself, and none of the above solutions work completely for me either.

    return Users::raw(function($collection)
    {
        return $collection->aggregate(
            [[
                '$lookup' => [
                    'as'=>'info',
                    'from'=>'beroep',
                    'foreignField'=>'voornaam',
                    'localField'=>'voornaam'
                ]
            ]]
        );
    });

This still is not working. At least not using the latest jenssegers mongodb with Laravel 5.3 and PHP 7.0. Switching to the PHP driver and my $lookup query works. jenssegers however, using Model::raw:(function($collection) { return $collection->aggregate( etc.... returns no results.

I'm using latest laravel 5.4 & mongodb 3.4 also still its not working.

Its still not resolved. It is good feature of mongodb

@pfzwaan it works, thanks.

@anggarasaja & @pfzwaan : Can you give mongodb php driver details & mongodb version details with laravel you used to got the result

Yes, $lookup feature needed to be able to use it with $match, $sort etc.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanjay1688 picture sanjay1688  路  3Comments

Vasiliy-Bondarenko picture Vasiliy-Bondarenko  路  3Comments

geofflancaster picture geofflancaster  路  3Comments

lgt picture lgt  路  3Comments

sebastiaanluca picture sebastiaanluca  路  3Comments