Denys Siebov
Denys Siebov

Reputation: 340

Mongo lookup with condition

My project is using Laravel and jenssegers/laravel-mongodb ext. I have a query with lookup:

'$lookup' => [
    'from' => 'proposals',
    'localField' => '_id',
    'foreignField' => 'proposal_round_mongo_id',
    'as' => 'proposal'
]

Proposal has a property status. How can I "join" propsoals with select data with where status="active" condition?

Upvotes: 0

Views: 693

Answers (1)

Sachin Kumar
Sachin Kumar

Reputation: 3240

You can use the pipeline option to achieve this. However you will need to read the document of the extension which is you used. But as mongoshell you can do as follow

{
   $lookup:
     {
       from: <collection to join>,
       let: { <var_1>: <expression>, …, <var_n>: <expression> },
       pipeline: [ <pipeline to execute on the collection to join> ],
       as: <output array field>
     }
}

And specify condition inside pipeline. Read from the following link. https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries

Upvotes: 1

Related Questions