Is there a way to pass condition in eloquent that if key exists then don't send object from collection (in mongodb)?

Viewed 252

I am using jenssegers/laravel-mongodb, I have a collection segments, I don't want those objects to be send by eloquent which has key named 'unrooted' i.e. to pass a condition to check if 'unrooted' key is set in collection, so I want

$condition[' ? '] = false;  // $condition unrooted exists is false.
$segments = Segment::where($condition)->get();

I know that it can be done like getting all the objects pass the condition, and then

foreach($segments as $key => $segment){
    if(property_exists($segment, 'unrooted')){
        unset($segments[$key]);    
    }
} 
dd(array_values($segments->toArray());

But it is not efficient for me incase of large collection. Thankyou for you help.

1 Answers

It was simple, just used mongodb docs, posting it here for future references.

$condition['$exists'] = false; 

$exists does the trick.

Related