Undefined property: MongoDBDriverManager::$db












0















I have a local MongoDB database running on WAMP on Windows 10. I'm using PHP version 7.2.10, Apache 2.4.35, and MongoDB extension 1.5.3. I have a pretty simple web application that I'm testing, and when I try to send data to my database via a php script, I get this error:



PHP Notice:  Undefined property: MongoDBDriverManager::$db in 
C:wamp64wwwphptest.php


Relevant parts of test.php, the file in question, look like this:



$objectId = new MongoDBBSONObjectId();
$dbhost = "127.0.0.1:27017";
$dbname = "db";
$m = new MongoDBDriverManager("mongodb://localhost:27017");
var_dump($m);
$db = $m->$dbname;


The property being undefined results in another error: Fatal error: Uncaught Error: Call to a member function selectCollection() on null which causes the script to fail.



What is causing the property MongoDBDriverManager::$db to be undefined?










share|improve this question

























  • use MongoDBClient instead of MongoDBDriverManager

    – Kazz
    Oct 27 '18 at 7:59











  • @Kazz yep, that's what I thought as well. Throws Fatal error: Uncaught Error: Class 'MongoDBClient' not found

    – Jpegzilla
    Oct 27 '18 at 7:59











  • your code the $m->$dbname also the ->selectCollection(...) correspond to legacy mongo but MongoDBDriverManager is part of newer mongodb so you have mismatch of two different libs in your code, see: php.net/manual/en/book.mongo.php (legacy) and php.net/manual/en/set.mongodb.php

    – Kazz
    Oct 27 '18 at 8:51













  • @Kazz thanks so much, that's defo what I was missing.

    – Jpegzilla
    Oct 27 '18 at 18:44
















0















I have a local MongoDB database running on WAMP on Windows 10. I'm using PHP version 7.2.10, Apache 2.4.35, and MongoDB extension 1.5.3. I have a pretty simple web application that I'm testing, and when I try to send data to my database via a php script, I get this error:



PHP Notice:  Undefined property: MongoDBDriverManager::$db in 
C:wamp64wwwphptest.php


Relevant parts of test.php, the file in question, look like this:



$objectId = new MongoDBBSONObjectId();
$dbhost = "127.0.0.1:27017";
$dbname = "db";
$m = new MongoDBDriverManager("mongodb://localhost:27017");
var_dump($m);
$db = $m->$dbname;


The property being undefined results in another error: Fatal error: Uncaught Error: Call to a member function selectCollection() on null which causes the script to fail.



What is causing the property MongoDBDriverManager::$db to be undefined?










share|improve this question

























  • use MongoDBClient instead of MongoDBDriverManager

    – Kazz
    Oct 27 '18 at 7:59











  • @Kazz yep, that's what I thought as well. Throws Fatal error: Uncaught Error: Class 'MongoDBClient' not found

    – Jpegzilla
    Oct 27 '18 at 7:59











  • your code the $m->$dbname also the ->selectCollection(...) correspond to legacy mongo but MongoDBDriverManager is part of newer mongodb so you have mismatch of two different libs in your code, see: php.net/manual/en/book.mongo.php (legacy) and php.net/manual/en/set.mongodb.php

    – Kazz
    Oct 27 '18 at 8:51













  • @Kazz thanks so much, that's defo what I was missing.

    – Jpegzilla
    Oct 27 '18 at 18:44














0












0








0








I have a local MongoDB database running on WAMP on Windows 10. I'm using PHP version 7.2.10, Apache 2.4.35, and MongoDB extension 1.5.3. I have a pretty simple web application that I'm testing, and when I try to send data to my database via a php script, I get this error:



PHP Notice:  Undefined property: MongoDBDriverManager::$db in 
C:wamp64wwwphptest.php


Relevant parts of test.php, the file in question, look like this:



$objectId = new MongoDBBSONObjectId();
$dbhost = "127.0.0.1:27017";
$dbname = "db";
$m = new MongoDBDriverManager("mongodb://localhost:27017");
var_dump($m);
$db = $m->$dbname;


The property being undefined results in another error: Fatal error: Uncaught Error: Call to a member function selectCollection() on null which causes the script to fail.



What is causing the property MongoDBDriverManager::$db to be undefined?










share|improve this question
















I have a local MongoDB database running on WAMP on Windows 10. I'm using PHP version 7.2.10, Apache 2.4.35, and MongoDB extension 1.5.3. I have a pretty simple web application that I'm testing, and when I try to send data to my database via a php script, I get this error:



PHP Notice:  Undefined property: MongoDBDriverManager::$db in 
C:wamp64wwwphptest.php


Relevant parts of test.php, the file in question, look like this:



$objectId = new MongoDBBSONObjectId();
$dbhost = "127.0.0.1:27017";
$dbname = "db";
$m = new MongoDBDriverManager("mongodb://localhost:27017");
var_dump($m);
$db = $m->$dbname;


The property being undefined results in another error: Fatal error: Uncaught Error: Call to a member function selectCollection() on null which causes the script to fail.



What is causing the property MongoDBDriverManager::$db to be undefined?







php mongodb wamp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 27 '18 at 8:09







Jpegzilla

















asked Oct 27 '18 at 7:53









JpegzillaJpegzilla

408




408













  • use MongoDBClient instead of MongoDBDriverManager

    – Kazz
    Oct 27 '18 at 7:59











  • @Kazz yep, that's what I thought as well. Throws Fatal error: Uncaught Error: Class 'MongoDBClient' not found

    – Jpegzilla
    Oct 27 '18 at 7:59











  • your code the $m->$dbname also the ->selectCollection(...) correspond to legacy mongo but MongoDBDriverManager is part of newer mongodb so you have mismatch of two different libs in your code, see: php.net/manual/en/book.mongo.php (legacy) and php.net/manual/en/set.mongodb.php

    – Kazz
    Oct 27 '18 at 8:51













  • @Kazz thanks so much, that's defo what I was missing.

    – Jpegzilla
    Oct 27 '18 at 18:44



















  • use MongoDBClient instead of MongoDBDriverManager

    – Kazz
    Oct 27 '18 at 7:59











  • @Kazz yep, that's what I thought as well. Throws Fatal error: Uncaught Error: Class 'MongoDBClient' not found

    – Jpegzilla
    Oct 27 '18 at 7:59











  • your code the $m->$dbname also the ->selectCollection(...) correspond to legacy mongo but MongoDBDriverManager is part of newer mongodb so you have mismatch of two different libs in your code, see: php.net/manual/en/book.mongo.php (legacy) and php.net/manual/en/set.mongodb.php

    – Kazz
    Oct 27 '18 at 8:51













  • @Kazz thanks so much, that's defo what I was missing.

    – Jpegzilla
    Oct 27 '18 at 18:44

















use MongoDBClient instead of MongoDBDriverManager

– Kazz
Oct 27 '18 at 7:59





use MongoDBClient instead of MongoDBDriverManager

– Kazz
Oct 27 '18 at 7:59













@Kazz yep, that's what I thought as well. Throws Fatal error: Uncaught Error: Class 'MongoDBClient' not found

– Jpegzilla
Oct 27 '18 at 7:59





@Kazz yep, that's what I thought as well. Throws Fatal error: Uncaught Error: Class 'MongoDBClient' not found

– Jpegzilla
Oct 27 '18 at 7:59













your code the $m->$dbname also the ->selectCollection(...) correspond to legacy mongo but MongoDBDriverManager is part of newer mongodb so you have mismatch of two different libs in your code, see: php.net/manual/en/book.mongo.php (legacy) and php.net/manual/en/set.mongodb.php

– Kazz
Oct 27 '18 at 8:51







your code the $m->$dbname also the ->selectCollection(...) correspond to legacy mongo but MongoDBDriverManager is part of newer mongodb so you have mismatch of two different libs in your code, see: php.net/manual/en/book.mongo.php (legacy) and php.net/manual/en/set.mongodb.php

– Kazz
Oct 27 '18 at 8:51















@Kazz thanks so much, that's defo what I was missing.

– Jpegzilla
Oct 27 '18 at 18:44





@Kazz thanks so much, that's defo what I was missing.

– Jpegzilla
Oct 27 '18 at 18:44












1 Answer
1






active

oldest

votes


















1














Working php code looks such as the following. Please, pay attention to the presence a link to 'vendor/autoload.php':



  $DB_CONNECTION_STRING="mongodb://YourCredentials";
require '../../vendor/autoload.php';
$manager = new MongoDBDriverManager( $DB_CONNECTION_STRING );


Then if you use MongoDBDriverManager, a modern version of MongoDB driver, your CRUD operations will look such as this:



Create a document in the collection:



$bulkWrite=new MongoDBDriverBulkWrite;
$doc=['name' => 'John', age => 33, profession => 'Guess what?'];
$bulkWrite->insert($doc);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);


Read document in the collection by name with a limit:



$filter = ['name' => 'John'];
$options = [ 'limit' => 2 ];
$query = new MongoDBDriverQuery($filter, $options);
$manager->executeQuery('db.MyCollection', $query);


Read document in the collection by MongoDb _id with a limit:



$filter = ['_id' => new MongoDBBSONObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = [ 'limit' => 2 ];
$query = new MongoDBDriverQuery($filter, $options);
$manager->executeQuery('db.MyCollection', $query);


Update document in the collection: (Read more about options upsert and multi here)



$bulkWrite=new MongoDBDriverBulkWrite;
$filter = ['name' => 'John'];
$update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
$options = array('multi' => false, 'upsert' => false);
$bulkWrite->update($filter, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);


Delete document in the collection - Delete:



$bulkWrite=new MongoDBDriverBulkWrite;
$filter= ['name' => 'John', age => 35];
$options = [ 'limit' => 1 ];
$bulkWrite->delete($filter, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53019846%2fundefined-property-mongodb-driver-managerdb%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Working php code looks such as the following. Please, pay attention to the presence a link to 'vendor/autoload.php':



      $DB_CONNECTION_STRING="mongodb://YourCredentials";
    require '../../vendor/autoload.php';
    $manager = new MongoDBDriverManager( $DB_CONNECTION_STRING );


    Then if you use MongoDBDriverManager, a modern version of MongoDB driver, your CRUD operations will look such as this:



    Create a document in the collection:



    $bulkWrite=new MongoDBDriverBulkWrite;
    $doc=['name' => 'John', age => 33, profession => 'Guess what?'];
    $bulkWrite->insert($doc);
    $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


    Read document in the collection by name with a limit:



    $filter = ['name' => 'John'];
    $options = [ 'limit' => 2 ];
    $query = new MongoDBDriverQuery($filter, $options);
    $manager->executeQuery('db.MyCollection', $query);


    Read document in the collection by MongoDb _id with a limit:



    $filter = ['_id' => new MongoDBBSONObjectID( '5bdf54e6d722dc000f0aa6c2' )];
    $options = [ 'limit' => 2 ];
    $query = new MongoDBDriverQuery($filter, $options);
    $manager->executeQuery('db.MyCollection', $query);


    Update document in the collection: (Read more about options upsert and multi here)



    $bulkWrite=new MongoDBDriverBulkWrite;
    $filter = ['name' => 'John'];
    $update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
    $options = array('multi' => false, 'upsert' => false);
    $bulkWrite->update($filter, $options);
    $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


    Delete document in the collection - Delete:



    $bulkWrite=new MongoDBDriverBulkWrite;
    $filter= ['name' => 'John', age => 35];
    $options = [ 'limit' => 1 ];
    $bulkWrite->delete($filter, $options);
    $manager->executeBulkWrite('db.MyCollection', $bulkWrite);





    share|improve this answer






























      1














      Working php code looks such as the following. Please, pay attention to the presence a link to 'vendor/autoload.php':



        $DB_CONNECTION_STRING="mongodb://YourCredentials";
      require '../../vendor/autoload.php';
      $manager = new MongoDBDriverManager( $DB_CONNECTION_STRING );


      Then if you use MongoDBDriverManager, a modern version of MongoDB driver, your CRUD operations will look such as this:



      Create a document in the collection:



      $bulkWrite=new MongoDBDriverBulkWrite;
      $doc=['name' => 'John', age => 33, profession => 'Guess what?'];
      $bulkWrite->insert($doc);
      $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


      Read document in the collection by name with a limit:



      $filter = ['name' => 'John'];
      $options = [ 'limit' => 2 ];
      $query = new MongoDBDriverQuery($filter, $options);
      $manager->executeQuery('db.MyCollection', $query);


      Read document in the collection by MongoDb _id with a limit:



      $filter = ['_id' => new MongoDBBSONObjectID( '5bdf54e6d722dc000f0aa6c2' )];
      $options = [ 'limit' => 2 ];
      $query = new MongoDBDriverQuery($filter, $options);
      $manager->executeQuery('db.MyCollection', $query);


      Update document in the collection: (Read more about options upsert and multi here)



      $bulkWrite=new MongoDBDriverBulkWrite;
      $filter = ['name' => 'John'];
      $update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
      $options = array('multi' => false, 'upsert' => false);
      $bulkWrite->update($filter, $options);
      $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


      Delete document in the collection - Delete:



      $bulkWrite=new MongoDBDriverBulkWrite;
      $filter= ['name' => 'John', age => 35];
      $options = [ 'limit' => 1 ];
      $bulkWrite->delete($filter, $options);
      $manager->executeBulkWrite('db.MyCollection', $bulkWrite);





      share|improve this answer




























        1












        1








        1







        Working php code looks such as the following. Please, pay attention to the presence a link to 'vendor/autoload.php':



          $DB_CONNECTION_STRING="mongodb://YourCredentials";
        require '../../vendor/autoload.php';
        $manager = new MongoDBDriverManager( $DB_CONNECTION_STRING );


        Then if you use MongoDBDriverManager, a modern version of MongoDB driver, your CRUD operations will look such as this:



        Create a document in the collection:



        $bulkWrite=new MongoDBDriverBulkWrite;
        $doc=['name' => 'John', age => 33, profession => 'Guess what?'];
        $bulkWrite->insert($doc);
        $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


        Read document in the collection by name with a limit:



        $filter = ['name' => 'John'];
        $options = [ 'limit' => 2 ];
        $query = new MongoDBDriverQuery($filter, $options);
        $manager->executeQuery('db.MyCollection', $query);


        Read document in the collection by MongoDb _id with a limit:



        $filter = ['_id' => new MongoDBBSONObjectID( '5bdf54e6d722dc000f0aa6c2' )];
        $options = [ 'limit' => 2 ];
        $query = new MongoDBDriverQuery($filter, $options);
        $manager->executeQuery('db.MyCollection', $query);


        Update document in the collection: (Read more about options upsert and multi here)



        $bulkWrite=new MongoDBDriverBulkWrite;
        $filter = ['name' => 'John'];
        $update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
        $options = array('multi' => false, 'upsert' => false);
        $bulkWrite->update($filter, $options);
        $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


        Delete document in the collection - Delete:



        $bulkWrite=new MongoDBDriverBulkWrite;
        $filter= ['name' => 'John', age => 35];
        $options = [ 'limit' => 1 ];
        $bulkWrite->delete($filter, $options);
        $manager->executeBulkWrite('db.MyCollection', $bulkWrite);





        share|improve this answer















        Working php code looks such as the following. Please, pay attention to the presence a link to 'vendor/autoload.php':



          $DB_CONNECTION_STRING="mongodb://YourCredentials";
        require '../../vendor/autoload.php';
        $manager = new MongoDBDriverManager( $DB_CONNECTION_STRING );


        Then if you use MongoDBDriverManager, a modern version of MongoDB driver, your CRUD operations will look such as this:



        Create a document in the collection:



        $bulkWrite=new MongoDBDriverBulkWrite;
        $doc=['name' => 'John', age => 33, profession => 'Guess what?'];
        $bulkWrite->insert($doc);
        $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


        Read document in the collection by name with a limit:



        $filter = ['name' => 'John'];
        $options = [ 'limit' => 2 ];
        $query = new MongoDBDriverQuery($filter, $options);
        $manager->executeQuery('db.MyCollection', $query);


        Read document in the collection by MongoDb _id with a limit:



        $filter = ['_id' => new MongoDBBSONObjectID( '5bdf54e6d722dc000f0aa6c2' )];
        $options = [ 'limit' => 2 ];
        $query = new MongoDBDriverQuery($filter, $options);
        $manager->executeQuery('db.MyCollection', $query);


        Update document in the collection: (Read more about options upsert and multi here)



        $bulkWrite=new MongoDBDriverBulkWrite;
        $filter = ['name' => 'John'];
        $update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
        $options = array('multi' => false, 'upsert' => false);
        $bulkWrite->update($filter, $options);
        $manager->executeBulkWrite('db.MyCollection', $bulkWrite);


        Delete document in the collection - Delete:



        $bulkWrite=new MongoDBDriverBulkWrite;
        $filter= ['name' => 'John', age => 35];
        $options = [ 'limit' => 1 ];
        $bulkWrite->delete($filter, $options);
        $manager->executeBulkWrite('db.MyCollection', $bulkWrite);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 4:15

























        answered Nov 4 '18 at 21:51









        RomanRoman

        2,6161928




        2,6161928






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53019846%2fundefined-property-mongodb-driver-managerdb%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues