laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Raw query that returns select fields

Open skeezus opened this issue 8 years ago • 15 comments

I'm using the query to organize places by location but return only a few columns/fields from each object in the collection. Based on my understanding of Mongo queries, the raw statement in the code below should work, but it returns an empty array. If I remove the name field it returns all the objects in the collection as expected, but not as desired.

There's several open issues on this question with no relevant responses. Even a "hey idiot, do this" would be nice.

$shops_sorted = Shop::raw(function($collection) use ($latitude, $longitude) { return $collection->find([ 'status' => 'show', 'loc' => [ '$near' => [ $longitude, $latitude ] ] ], [ 'name' => 1 ] ); })->toArray();

skeezus avatar Jun 21 '17 19:06 skeezus

@skeezus did you have any luck with finding solution for this problem...?

usamamashkoor avatar Jul 10 '17 19:07 usamamashkoor

@usamamashkoor I did not. Current fix is to put the fields I need from the database query into an array before returning the response to the client.

skeezus avatar Jul 10 '17 19:07 skeezus

Thanks @skeezus i will try your solution Thanks again for replying in such a short time :)

usamamashkoor avatar Jul 10 '17 19:07 usamamashkoor

hey did you tried this

$shops_sorted = Shop::raw(function($collection) use ($latitude, $longitude) { return $collection ->find([ 'status' => 'show', 'loc' => [ '$near' => [ $longitude, $latitude ] ] ], [ 'name' => 1 ] ) ->fields(['field1' => true, 'field2' => true]) })->toArray();

har256b avatar Jul 27 '17 22:07 har256b

in case you need all fields from your collection but not a few then try setting the value to false and that field will be removed from result object

http://php.net/manual/en/mongocursor.fields.php

har256b avatar Jul 27 '17 22:07 har256b

@har256b How do you force mongo to use that php method?...I'm getting the following error:

"Call to undefined method MongoDB\Driver\Cursor::fields()"

skeezus avatar Aug 19 '17 19:08 skeezus

@skeezus sorry for responding late looks like you are using newer version of MongoDB Driver which actually doesn't support fields yet

Legacy http://php.net/manual/en/class.mongocursor.php

New http://php.net/manual/en/class.mongodb-driver-cursor.php

har256b avatar Aug 25 '17 09:08 har256b

Try something like this is new driver version

/* Select documents matching given criteria */
$filter = [
    'status' => 'show',
//    ...
];

$options = [
    /* Only return following fields from matching documents */
    'projection' => [
        'title' => true,
        'article' => true,
    ],
    /* Return the documents in specified order */
    'sort' => [
        // specify your sort criteria here
    ],
];

$query = new MongoDB\Driver\Query($filter, $options);
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');

$cursor = $manager->executeQuery($collection, $query);

foreach($cursor as $document) {
    // document here should have only specified fields along with id
    var_dump($document);
}

har256b avatar Aug 25 '17 09:08 har256b

with the package probably like this

$shops_sorted = Shop::raw(function($collection) use ($latitude, $longitude) { 
    return $collection ->find(['status' => 'show', 'loc' => [ '$near' => [ $longitude, $latitude ] ] ], ['field1' => true, 'field2' => true] );
})->toArray();

har256b avatar Aug 25 '17 09:08 har256b

also you need to be sure which Driver Version and Package Version you are using, baring this in mind WARNING: The old mongo PHP driver is not supported anymore in versions >= 3.0.

har256b avatar Aug 25 '17 09:08 har256b

You can use the php function iterator_to_array($resultOfQueryRaw) that will return array with MongoDB\Model\BSONDocument objects then you do what you need.

josemiguelq avatar May 09 '18 20:05 josemiguelq

Raw expression with Eloquent generates conflics, I wrote an issue #1640 . When I use a eloquent raw expression and I make toArray() delete some columns because in dates variable exists a column that is inside result, but if I remove value con array, it works "well".

joerecra avatar Nov 03 '18 04:11 joerecra

@har256b it didn't work. I tried to get a single value but it's returning all the fields.

Abhinanddas avatar Jul 23 '19 12:07 Abhinanddas

It should be

$shops_sorted = Shop::raw(function($collection) use ($latitude, $longitude) { 
    return $collection ->find(
                    ['status' => 'show', 'loc' => [ '$near' => [ $longitude, $latitude ] ] ], 
                    [ 'projection' => ['field1' => 1, 'field2' => 1]] );
});

giaesp avatar Mar 23 '20 09:03 giaesp

xcv

hiteshvaghanii avatar Nov 15 '21 06:11 hiteshvaghanii

I could not reproduce the bug with the current version.

GromNaN avatar Aug 28 '23 15:08 GromNaN