2016-10-07 12:21
浏览 52


I am on mongoDB 3 and php version 5.6. I want to create a database for adding data in it. I am trying in this way.


    require 'vendor/autoload.php';

   // connect to mongodb

   $db = new MongoDB\Client("mongodb://localhost:27017");

   echo "Connected";

   // select a database
   $data = $db->selectDatabase("admin");

   echo "Done ";

This code is running well but not making any database in the MongoDB. Can someone help?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongliechuich10319 2016-10-07 14:53

    I hope you have installed Mongodb driver for PHP and you are not receiving any exception while executing your code.

    After the db connection is established you need to save something in collection so that the db comes to existence. This is missing in your code.

    Try below code

       // connect to mongodb
       $m = new MongoClient();
       echo "Connection to database successfully";
       // select a database
       $db = $m->admin;
       echo "Database admin selected";
       $collection = $db->createCollection("mycol");
       echo "Collection created succsessfully";
    打赏 评论

相关推荐 更多相似问题