dongmao3131 2016-10-07 12:21
浏览 52
已采纳

使用php在mongoDB中创建数据库

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.

<?php

    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

    <?php
       // 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";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退