1

I'm testing out mongodb with php and I have run in to a problem I don't know how to solve, this is the code I am using at the moment.

<?php

require ($_SERVER['DOCUMENT_ROOT'] . '/grafic/mongodb_php/vendor/autoload.php'); 

$client = new MongoDB\Client;
$snrdb = $client->snrdb;
$nodos_live = $snrdb->nodos_live;

$filter  = [];
$options = ['sort' => ['_id' => 1], 'limit' => 1];

$show = $nodos_live->find($filter, $options);

foreach ($show as $collection) {
    $snr_1= $collection["n1_snr_1"];
    $snr_2= $collection["n1_snr_2"];
}

echo $snr_1 ." ";
echo $snr_2;

?>

this gives me

28.9 28.3

now if I change the id and collection name to 2 and n2_snr_* like so.

<?php

require ($_SERVER['DOCUMENT_ROOT'] . '/grafic/mongodb_php/vendor/autoload.php'); 

$client = new MongoDB\Client;
$snrdb = $client->snrdb;
$nodos_live = $snrdb->nodos_live;

$filter  = [];
$options = ['sort' => ['_id' => 2], 'limit' => 1];

$show = $nodos_live->find($filter, $options);

foreach ($show as $collection) {
    $snr_1= $collection["n2_snr_1"];
    $snr_2= $collection["n2_snr_2"];
}

echo $snr_1 ." ";
echo $snr_2;

?>

this gives me this ERROR msg:

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionException: bad sort specification in C:\xampp\htdocs\grafic\mongodb_php\vendor\mongodb\mongodb\src\Operation\Find.php:287 Stack trace: #0 C:\xampp\htdocs\grafic\mongodb_php\vendor\mongodb\mongodb\src\Operation\Find.php(287): MongoDB\Driver\Server->executeQuery('snrdb.nodos_liv...', Object(MongoDB\Driver\Query), Array) #1 C:\xampp\htdocs\grafic\mongodb_php\vendor\mongodb\mongodb\src\Collection.php(531): MongoDB\Operation\Find->execute(Object(MongoDB\Driver\Server)) #2 C:\xampp\htdocs\grafic\test\test.php(23): MongoDB\Collection->find(Array, Array) #3 {main} thrown in C:\xampp\htdocs\grafic\mongodb_php\vendor\mongodb\mongodb\src\Operation\Find.php on line 287

And I can't understand why I get this ERROR msg anyone that can help me with this?

If a do a dump like so

<?php 

require ($_SERVER['DOCUMENT_ROOT'] . '/grafic/mongodb_php/vendor/autoload.php'); 

$client = new MongoDB\Client;
$snrdb = $client->snrdb;
$nodos_live = $snrdb->nodos_live;

$show = $nodos_live->findOne(
    ['_id' => '1']
);

var_dump($show)

?>

I get this showing that ther is a document with id 1

object(MongoDB\Model\BSONDocument)#19 (1) {
  ["storage":"ArrayObject":private]=>
  array(5) {
    ["_id"]=>
    string(1) "1"
    ["n1_snr_1"]=>
    float(28.9)
    ["n1_snr_2"]=>
    float(28.3)
    ["n1_snr_3"]=>
    string(1) "0"
    ["time_stamp"]=>
    string(19) "2018-05-31 21:44:22"
  }
}

And same thig I i do a dump of _id => 2 it shows that it exist as well.

object(MongoDB\Model\BSONDocument)#19 (1) {
  ["storage":"ArrayObject":private]=>
  array(5) {
    ["_id"]=>
    string(1) "2"
    ["n2_snr_1"]=>
    float(31.7)
    ["n2_snr_2"]=>
    float(35.7)
    ["n2_snr_3"]=>
    string(1) "0"
    ["time_stamp"]=>
    string(19) "2018-05-31 21:44:22"
  }
}

1 Answer 1

2

sort can only take two values 1 for ascending order and -1 for descending order. If you want to query documents with _id=2, you have to specify that in the query, not in the sort criteria.

Sign up to request clarification or add additional context in comments.

1 Comment

Baaahhhhaa.. of course your right it need to be $filter = ['_id' => '2']; $options = ['sort' => ['_id' => 1], 'limit' => 1]; $show = $nodos_live->find($filter, $options);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.