thephpleague / monga

Simple and swift MongoDB abstraction.
MIT License
331 stars 30 forks source link

collection find function #23

Closed shahmanthan9 closed 9 years ago

shahmanthan9 commented 9 years ago

When I was trying to fetch all documents from one of my collection without where clause and with fields i want, it always returned an empty array. I checked and found that issue in Collection.php line number 317 and 318.

dimasdanz commented 9 years ago

I can confirm this Here's the code I use

$collection->find(function($query) {
    $query->select(
        'field1',
        'field2',
        'field3'
    );
    $query->orderBy('updated_at', 'desc');
    $query->limit(10); 
});

Code above return empty array. But it works if it's like this

$collection->find(function($query) {
    $query->where('field', 'condition')
    $query->select(
        'field1',
        'field2',
        'field3'
    );
    $query->orderBy('updated_at', 'desc');
    $query->limit(10); 
});
bcrowe commented 9 years ago

How it currently stands is that $query->where() and $query->select() are implicitly inclusive of one another. Whenever one or another is called, the $arguments variable will no longer be an empty array, subsequently furnishing the underlying MongoCollection::find($query, $fields) call with ordinal arguments via call_user_func_array. So, what's happening in your cases, your select() calls in turn actually are getting supplied as MongoCollection::find()'s $query parameter, resulting in the empty result set.

I do agree that it shouldn't behave this way, but I worry changing this behavior outright could explode some people's applications in the case anyone was ever dependent on this behavior. I'll have a little look/think about how to maybe squeeze some sort of workaround into a minor version.

shahmanthan9 commented 9 years ago

Thanks @bcrowe

pascal-hofmann commented 9 years ago

I'm running into the same issue right now. Collection::find should be fixed, so that it never uses $fields as $query. If you want to avoid breaking older applications, I suggest increasing the major version. With this bug monga is useless for more complex applications.

bcrowe commented 9 years ago

I agree. I think bumping up a minor version is fine here to respect any existing ~1.1 pins. Closing as there's a PR open now.