To use MongoDB with PHP, you need to use MongoDB PHP driver.
Add extension = php_mongo.dll in php.ini file
$m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb;It will create the connection & also select the database. if Database is not available then it will create automatically.
$collection = $db->createCollection("mycol");
$db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.tutorialspoint.com/mongodb/", "by" => "tutorials point" ); $collection->insert($document);
$cursor = $collection->find();
// iterate cursor to display title of documents
foreach ($cursor as $document)
{
echo $document["title"] . "\n";
}
// It will find all the collections
$collection->update // to update the collection
$collection->remove(array("title"=>"MongoDB Tutorial"),false); // To remove the collection
Comments
Post a Comment