laravel-mongodb
laravel-mongodb copied to clipboard
Raw query that returns select fields
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 did you have any luck with finding solution for this problem...?
@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.
Thanks @skeezus i will try your solution Thanks again for replying in such a short time :)
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();
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 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 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
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);
}
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();
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.
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.
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".
@har256b it didn't work. I tried to get a single value but it's returning all the fields.
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]] );
});
xcv
I could not reproduce the bug with the current version.