Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:29:27+00:00 2026-06-11T17:29:27+00:00

I have a mongo class, which I later want to add CRUD functions into

  • 0

I have a mongo class, which I later want to add CRUD functions into it, but as of now I want to write my own collection functions after I created objoect of Mongo, so anytime I want to use mongodb I just create object of my class, and write the commands

but it gives me this error:

Exception raised : Error (2) : “MongoCollection::__construct() expects parameter 1 to be MongoDB, object given

How can I get it as MongoDB,

mongo.class.php

   class Mongo
   {
        public function __construct(){
               $this->connect();
        }
        public function connect{
                $this->conn = new \Mongo("mongodb://admin:123456@192.168.2.3); 
                $this->dbLink = $this->conn->selectDB('profiles');
                return $this->dbLink;
    }

index.php

       $myMongo = new Mongo(); 
       $collection = new MongoCollection($myMongo,'user');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:29:28+00:00Added an answer on June 11, 2026 at 5:29 pm

    I suppose the problematic line should be written like this:

    $collection = new MongoCollection($myMongo->dbLink, 'user');
    

    You seem to expect that Mongo class constructor will return the value returned by connect method. But that’s just not true: constructor returns the whole object (which properties may – and may be not – defined as it executes).

    Some might say that dbLink property should not be exposed directly, and getter method should be used instead:

    private $dbLink;
    ...
    public function getDb() {
      return $this->dbLink;
    }
    ...
    $collection = new MongoCollection($myMongo->getDb(), 'user');
    

    Me, I don’t think this is necessary in this case, as your class seems to be tight-coupled with Mongo itself (which is, by me, what should be fixed first).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mongo collection which has documents with two fields fieldA and fieldB,
I have a collection in mongo in which I am inserting data from Perl
I have a MongoService class public class MongoService { private final Mongo mongo; private
I have a MongoService class as public class MongoService { private final Mongo mongo;
The problem is: I have 3 mongo tables, and I want to migrate data
I have an existing mongo database where all documents in a collection have a
I have a Mongo DB column which contains values of the sort 1001,100 and
I have a simple mongodb collection which stores tweets directly from the twitter api
Inside my class I have a private variable that my methods use which has
I have a .NET 4.0 class libary, which only uses .Net 2.0 features. To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.