10

I am running a mongodb on a linux box. So every time I connect to it from the console (typing mongo) I get something like this:

MongoDB shell version: 2.4.9
connecting to: test

And then I am doing use myDatabase (where myDatabase is 99% is the same). So basically I always do some unneeded type of work. Is there a way to configure mongo, so that it will connect to myDatabase by default?

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

4 Answers4

17

Surprised that I don't find a duplicate of this. Okay, now we have content.

From the command line, just do this:

$ mongo myDatabase

This actually is covered in the documentation, albeit down the page somewhat. No direct link but search for <db address> and the same example is there.

Of course you could have done:

$ mongo --help
MongoDB shell version: 2.4.9
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
    foo                   foo database on local machine
    192.169.0.5/foo       foo database on 192.168.0.5 machine
    192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999

Which shows the usage along with other options you can pass in.

Another thing, not quite a default connect but a shortcut is you can do this in the .mongorc.js file:

db=db.getSiblingDB("myDatabase")

Which assigns the variable db to that database so now:

db.collection.find()

Is acting on myDatabase.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • 1
    Thanks, but I knew about mongo myDatabase, but for me this is actually the same thing: as I still need to type the name of my database. Sorry that I have not written about it in my question. – Salvador Dali Mar 15 '14 at 00:55
  • @SalvadorDali Okay then, more info in the edit that should suit. – Neil Lunn Mar 15 '14 at 01:04
3

As per latest mongodb drivers, we can provide default database name in connection string like this:

1. Connect using the mongoShell

mongo "mongodb+srv://sandbox.ununu.mongodb.net/mydatabase" --username user001

2. Connect using the drivers

mongodb+srv://user001:<password>@sandbox.ununu.mongodb.net/mydatabase?retryWrites=true&w=majority

Details of parametere:

  • Replace mydatabase with the name of the database that connections will use by default.
  • sandbox.ununu.mongodb.net is your cluster name
  • You will be prompted for the password for the Database User,user001.
  • user001 is username

For more details on passing ReplicaSet, other query string parameters, etc. refer mongodb official document. document link

KushalSeth
  • 3,265
  • 1
  • 26
  • 29
0

Usually mongodb connects to default database test. It's very simple connect to any database, we just need to specify the new database name in the mongo shell

  1. With authentication enabled and enter password on runtime (preferred mostly)

    mongo --port 27017 -u USERNAME DATABASE_NAME --authenticationDatabase admin
    
  2. With authentication enabled and password supplied on shell (not recommended)

    mongo --port 27017 -u USERNAME -p PASSWORD DATABASE_NAME --authenticationDatabase admin
    
  3. With authentication disabled

    mongo --port 27017 -u USERNAME -p PASSWORD DATABASE_NAME
    

The above commands will directly connect to the database which we have specified.

Note: make sure the user has the necessary privilege to perform the operations

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jerry
  • 7,863
  • 2
  • 26
  • 35
0
mongosh
use admin
db.createUser({ user: "user" , pwd: "1234", roles: [ "readWrite", "dbAdmin" ] })

use metadata
db.createCollection("product")

// mongodb://user:1234@localhost:27017/metadata
seunggabi
  • 1,699
  • 12
  • 12