Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ryank30's avatar

How to insert data on Mongodb

Hi, I am thinking to create a collection and insert records to my mongodb when the data is posted to my controller. I have installed jenssegers/laravel-mongodb

In my controller I am using this code bit below.

DB::connection('mongodb')->collection('accounts')->insert($insRow);

In my config/database.php

    'mongodb' => [
        'driver'   => 'mongodb',
        'host'     => env('DB_HOST', 'localhost'),
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE', 'mydb'),
        'username' => env('DB_USERNAME', 'user'),
        'password' => env('DB_PASSWORD', 'password'),
        'options' => [
            'db' => 'admin' // sets the authentication database required by mongo 3
        ]
    ], 

When I ran it, I got an error message saying: AuthenticationException in InsertMany.php line 116: auth failed

Thanks in advance for your help.

0 likes
13 replies
ryank30's avatar

@willvincent Thanks for your advice. I already have created an admin user to my database but I don't think I have enabled it.

ryank30's avatar

@willvincent I ran code below to see the database connection.

dd(DB::connection('mongodb'));

Th db name was my mysql db name instead of my mongodb name. Does it mean DB:: connection is failing to pick up my mongodb?

ryank30's avatar

I gave authentication by running

mongo -u user -p pass --authenticationDatabae admin

Also i have set up user password on admin collection.

If this is not correct, could you advise how to enable auth please?

Thanks.

jimmck's avatar

@ryank30 You are running Mongo locally on your machine? By default passing null to the connection uses Mongo locally. Otherwise you need the URL and port to connect. If you downloaded mongo directly and started there is no authentication needed. You also need to know the database name and collection.

ryank30's avatar

@jimmck the mongodb is running on my virtual server. Could you show me the example code please then?

jimmck's avatar

@ryank30 I don't use that Laravel library. Your config settings do not match the settings on the Git page for this package. When you install Mongo, it only installes itself. There is are no default databases or collections. Did you run the examples in the PHP doc for the Mongo driver enhancements? Did you install the mongo driver? The examples in the manual will work out of the box if you have Mongo running. How is the database mydb listed in your config being added to mongo? Go to www.robomongo.org and download the tool. You can easily check to see if you have a running server and what databases and collections are there.

ryank30's avatar

@jimmck

I have set up a fresh new laravel to test mongodb as a second db. Here is how I did. Please see below.

In Conroller:

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use Form; use App\Account;

class ProjectController extends Controller {

public function process(Account $account)
{
    $data = array('name' => 'Ryan', 'email' => 'test@ryan.com');
    $account->insertData($data);
    return 'done';
}

}

In my model:

namespace App; use Jenssegers\Mongodb\Eloquent\Model as Eloquent; use DB;

class Account extends Eloquent { protected $connection = 'mongodb';

 public function insertData($array)
 {
    
    dd(DB::connection('mongodb'));
    $insertData = DB::collection('accounts')->insert($array);
    if($insertData){
        return true;
    }
 }

}

When I ran it, the error message said "Unsupported driver [mongodb]"

So I went over to my server, which is running on my virtualbox, and ran this command line: "php --ri mongodb | grep version" to see the version of my php mongo driver. It printed like below:

mongodb version => 1.1.2 libmongoc version => 1.3.1-dev libbson version => 1.3.0

I think this is the latest version and it shouldn't create any issue.

If you don't mind, could you help me one more time please?

My ideal environment is that I could write a raw mongo query as opposed to create collections and insert data. For example, I would send an array data and then insert them to my mongodb by running db.mycollection.insert().

Thanks.

MaCo117's avatar

@ryank30 would you mind sharing how did you manage to solve the "Unsupported driver [mongodb]" error please? Thanks very much.

MaCo117's avatar

Thank you for interesting resource. The problem was incompatibility of laravel-mongodb driver with laravel-couchdb driver. We were at the time comparing multiple database solutions to choose the one for our project - therefore the two drivers. After uninstalling couchdb driver from laravel, we got it working. Thanks for your help.

Please or to participate in this conversation.