Match and lookup in mongo

22:40
we can easily combine one or more documents result set based on foreign key relation in mongo so please follow below procedure to achieve this. we are using following concepts to join documents in mongo
  • Aggregate
  • pipeline
  • lookup

Mongo Connection

create config.php to establish connection with mongo
<?php 
$dbhost = 'localhost';
$m = new Mongo("mongodb://$dbhost:27017");  
$db = $manager->selectDB("mydba");  
?>

Aggregate

Match operation performs on both collections in mongo.
<?php
//connection db
include("config.php");

$collection1= 'test';

$collection2= 'transactions';

$my_collection = $db->selectCollection($collection1);

//pipeline
$pipeline = array(


    //lookup
    array('$lookup' => array('from'=>$collection2,

                                'localField'=>'user_id',

                                'foreignField'=>'user_id',

                                'as'=>'Transactions')),

   array('$match'=>array('Transactions'=>['$ne' => []])),

   array('$addFields'=>array(
                            "Transactions"=>[
                                '$arrayElemAt' =>[
                                    ['$filter' => ["input" => '$Transactions',
                                                    "as" => "tran",
                                                    "cond" => ['$eq'=>['$$tran.quantity','1']]
                                                    ]],0
                                ]
                            ]

                        )),


    //limit
    array('$limit' => 10),

    //skip
    array('$skip' => 0)


);

$cursor = $my_collection->aggregate($pipeline,array('cursor'=>array('batchSize'=>10000)));

echo '<pre>';
print_r($cursor['cursor']['firstBatch']);
echo '</pre>';
Output:
Array
(
    [0] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 5b20e0ba33e06a192c323fe7
                )

            [user_id] => 1
            [name] => sekhar
            [age] => 25
            [state] => AP
            [country] => India
            [Transactions] => Array
                (
                    [_id] => MongoId Object
                        (
                            [$id] => 5b20f50933e06a192c323feb
                        )

                    [user_id] => 1
                    [amount] => 20
                    [quantity] => 1
                    [product] => Book1
                )

        )

    [1] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 5b20f04333e06a192c323fe9
                )

            [user_id] => 2
            [name] => tester
            [age] => 22
            [state] => TN
            [country] => USA
            [Transactions] => Array
                (
                    [_id] => MongoId Object
                        (
                            [$id] => 5b20f56133e06a192c323fec
                        )

                    [user_id] => 2
                    [amount] => 30
                    [quantity] => 1
                    [product] => book2
                )

        )

)
 

No comments:

Powered by Blogger.