I use this code in the MongoDB PHP driver to get all documents in the database
$result = $collection->find();
foreach ($result as $doc) {
print_r($doc);
}
However, when adding a limit to it, it doesn't work anymore: no documents get printed anymore:
$result = $collection->find()->limit(10);
foreach ($result as $doc) {
print_r($doc);
}
There are certainly enough documents in the database. I cannot figure out what the problem with this is.
I have fixed the problem by taking a look at the source of the beta version. The documentation only appeared to be for the legacy mongo extension and not the newer mongodb extension.
The error logs showed this: Call to undefined method MongoDB\\Driver\\Cursor::addOption(). I checked out the documentation and concluded the function should have worked because it said (PECL mongo >=0.9.0). Note the missing db after mongo.
I fixed it by doing:
$collection->find([], [ 'limit' => 2 ]);, providing an empty filters array and adding my options in another array afterwards.
I am trying to describe with example for new php mongodb driver. showing in example skip,limit,Fields slection
require 'vendor/autoload.php'; // include Composer's autoloader
$client = new MongoDB\Client("mongodb://localhost:27017");
// SELECT * FROM YOUR_TABLE_NAME ;
// db.YOUR_COLLECTION_NAME.find({});
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array());
//SELECT * from YOUR_TABLE_NAME WHERE YOUR_COLUMN = "A"
// db.YOUR_COLLECTION_NAME.find({{ YOUR_FIELD: "A" }});
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('YOUR_FIELD'=>'A'));
//Return the Specified Fields and the _id Field Only
//SELECT _id, item,status YOUR_TABLE_NAME from inventory WHERE status = "A"
//db.YOUR_COLLECTION_NAME.find( { status: "A" }, { item: 1, status: 1 } )
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('status'=>'A'),array('projection' =>array('item'=>TRUE,'status' => TRUE)));
//Suppress _id Field
//SELECT item, status from YOUR_TABLE_NAME WHERE status = "A"
//db.YOUR_COLLECTION_NAME.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('status'=>'A'),array('projection' =>array('item'=>TRUE,'status' => TRUE,'_id'=>FALSE)));
//SELECT * FROM YOUR_TABLE_NAME LIMIT 10
//db.YOUR_COLLECTION_NAME.find({}).limit(10);
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array(),array('limit'=>10));
//SELECT * FROM YOUR_TABLE_NAME LIMIT 5,10
//db.YOUR_COLLECTION_NAME.find({}).skip(5).limit(10)
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array(),array('skip'=>5,'limit'=>10));
//Suppress _id Field
//SELECT item, status from YOUR_TABLE_NAME WHERE status = "A" LIMIT 5,10;
//db.YOUR_COLLECTION_NAME.find( { status: "A" }, { item: 1, status: 1, _id: 0 } ).skip(5).limit(10);
$result = $clinet->YOUR_DB_NAME->YOUR_COLLECTION_NAME->find(array('status'=>'A'),array('projection' =>array('item'=>TRUE,'status' => TRUE,'_id'=>FALSE),'skip'=>5,'limit'=>10));
foreach ($result as $entry){
echo "<pre>";
print_r($entry);
echo "</pre>";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With