Reputation: 11
I am working on a project which involves PHP and mongodb integration. The aim of my code it to retrieve the latest inserted document. The code is selecting the database but not retrieving the required document. How do I modify the current code to achieve my aim?
I have tried many codes but they don't work. I am working on Ubuntu VPS by hostinger.
<?php
require 'vendor/autoload.php';
include 'lib/JSON.php';
// connect to mongodb
$client = new MongoDB\Client("mongodb://localhost:27017");
echo "Connection to database successfully";
// select a database
$db = $client->car_data;
$collection = $db->accident;
echo "\nDatabase car_data selected";
$result = $collection->find()->sort(array('_id' => -1));
$myJson = json_encode($result);
echo $myJson;
?>
For the above code, the page is loading but the only database is getting selected data is not being retrieved.
Upvotes: 0
Views: 286
Reputation: 614
For Update:
$r=$collection->updateOne(<filter>,<set>);
echo '<br>'.$r->getUpsertedId();
echo '<br>'.$r->getUpsertedCount();
MongoDB\UpdateResult::getMatchedCount()
MongoDB\UpdateResult::getModifiedCount()
MongoDB\UpdateResult::getUpsertedCount()
MongoDB\UpdateResult::getUpsertedId()
MongoDB\UpdateResult::isAcknowledged()
For insert lastinsertid:
echo $r->getInsertedId();
MongoDB\InsertOneResult::getInsertedCount()
MongoDB\InsertOneResult::getInsertedId()
MongoDB\InsertOneResult::isAcknowledged()
Upvotes: 0