top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use mongoDB with php?

+2 votes
455 views

I am new to mongodb but I know php so I want to implement both and develop a single page application using php and mongoDB.A sample code will be helpful for me like.CRUD Operation

posted Mar 6, 2016 by Shivam Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

To use mongodb with php you need to use mongodb php driver. You can download them from here -
https://s3.amazonaws.com/drivers.mongodb.org/php/index.html

Edit php.ini so that it can load mongo driver. Add this line(in Windows)

extension=php_mongo.dll

Now coming to the sample code Snipset

<?php

   // connect to mongodb
   $m = new MongoClient();
   echo "Connection Successful";

   // select a database
   $db = $m->mydb;
   echo "Database Selected";
   $collection = $db->mycol;
   echo "Collection Selected";

   $document = array( 
      "title" => "MongoDB", 
      "description" => "database", 
      "likes" => 100,
      "url" => "http://www.QueryHome.com/mongodb/",
      "by", "Salil"
   );

   $collection->insert($document);
   echo "Document inserted successfully";
?>

Refer the following link also would be helpful -
https://codezone4.wordpress.com/2013/09/11/simple-crud-with-mongodb-and-php/

answer Mar 7, 2016 by Salil Agrawal
Thank you!!
Similar Questions
+1 vote

I recently upgraded the latest PHP driver and could do an "explain" query by using the "$explain" modifier when executing the query, however as of MongoDB 3.2 the operator was removed so this is no longer possible.

So my question is how can I do an explain query with the new driver and MongoDB 3.2?

+3 votes

I am very new to mongodb, my requirement is need to search a value in two fields. ex: first_name, last_name

I am using PHP as serverside and mongodb as backend.

Can you please suggest, How can I write the query in PHP.

Below is the things, I tried to implement but not succeeded.

db.users.aggregate(
  [
  { $project: { userName: { $concat: [ "$first_name", " - ", "$last_name" ] } } }
  ]
)
+1 vote

I am able to run the web application using php and mongodb 3.2.6 in localhost.But I need to deploy mongodb in existed servers like Hostgator or Godaddy, then I need to run the php(5.4) web application.

Is it possible? Please suggest me to run my php application to connect mongodb?

...