Is it possible to group databases???

62 views
Skip to first unread message

Harshaa Subramani

unread,
Sep 24, 2015, 12:00:49 PM9/24/15
to mongodb-user
Hi, 

    I need to create multiple groups of same databases, such that each group has same set of database names with different data stored. Also in which authentication can be done to the separate group.

Stephen Steneker

unread,
Sep 24, 2015, 8:52:11 PM9/24/15
to mongodb-user

On Friday, 25 September 2015 02:00:49 UTC+10, Harshaa Subramani wrote:

    I need to create multiple groups of same databases, such that each group has same set of database names with different data stored. Also in which authentication can be done to the separate group.

Hi Harshaa,

A MongoDB server supports multiple databases which each contain multiple collections, so you could set up a database per client/group which has your standard set of collections, eg:

 db1.profiles
 db1.posts
 ...
 db2.profiles
 db2.posts
 ...

Role-based access control can also be configured to suit your requirements:

Regards,
Stephen

A. Jalil @AJ

unread,
Sep 25, 2015, 1:43:06 AM9/25/15
to mongodb-user
Good to know, great way to manage DBs and grant roles to appropriate users accordingly..

One thing I found out by accident, I wanted to use [config] DB, I did a typo by typing: "use conf" instead of "use config", and next thing a database called [conf] got created which I thought it was a bad idea, I should have gotten an error message telling me that [conf] does not exist. Here is why ?  If I have few developers accessing the Prod databases to run their procedures and if they keep making mistakes, my database can turn to a mess..  Is there a way to prevent "use dbname" form getting created if it doesn't exist ? what is the best way to prevent users to create empty databases if they make mistakes ?

Here is the other dangerous command:  db.dropDatabase() - If the user forgot which database he/she is using, they can easily drop a critical database.. I am assuming there is a user role I can grant to users so they can manage and drop their own database, but they should not touch the other databases, right ?

Better yet, it would be great if the database name can be used within the dropDatabase command:  exp:  db.dbname.dropDatabase() - is there a such command ?

Just trying to find better ways to manage mongo databases effectively in hopes to prevent any potential accidents..

Thanks.
@AJ


Harshaa Subramani

unread,
Sep 25, 2015, 2:03:47 AM9/25/15
to mongodb-user
  
   Thanx Stephen

Stephen Steneker

unread,
Sep 25, 2015, 9:23:52 AM9/25/15
to mongodb-user

On Friday, 25 September 2015 15:43:06 UTC+10, A. Jalil @AJ wrote:

One thing I found out by accident, I wanted to use [config] DB, I did a typo by typing: “use conf” instead of “use config”, and next thing a database called [conf] got created which I thought it was a bad idea, I should have gotten an error message telling me that [conf] does not exist.

Hi AJ,

This is an intended feature of MongoDB for agile development: you do not have to explicitly create a database or collection. When you typo’d use conf, this selected the namespace for context in the mongo shell but did not allocate any files on disk. The data files will be created on disk when you first insert data into a collection or take an action that requires the namespace to exist, such as adding indexes or enabling profiling.

Here is why ? If I have few developers accessing the Prod databases to run their procedures and if they keep making mistakes, my database can turn to a mess.. Is there a way to prevent “use dbname” form getting created if it doesn’t exist ? what is the best way to prevent users to create empty databases if they make mistakes ?

Empty databases shouldn’t be an issue, as they are not actually created on disk. To avoid random databases being created you need to lock down your user roles & permissions. Most users and applications will not need read/write access at the cluster level.

Here is the other dangerous command: db.dropDatabase() - If the user forgot which database he/she is using, they can easily drop a critical database.. I am assuming there is a user role I can grant to users so they can manage and drop their own database, but they should not touch the other databases, right ?

MongoDB supports Role-Based Access Control using system-defined and user-defined roles. For more information, check out the very detailed Security section in the MongoDB manual and the Security Checklist.

Better yet, it would be great if the database name can be used within the dropDatabase command: exp: db.dbname.dropDatabase() - is there a such command ?

Your example syntax wouldn’t make sense given that db is already a database object (the dbname would be assumed to be a collection name). However, there is a more verbose version you could use if you want to explicitly name a database:

  db.getSiblingDB('dates').dropDatabase()

Most of the shell helpers are written in JavaScript (although a few are native C++ functions) so you can see how they are currently implemented by running the function without parentheses, eg:

> db.dropDatabase
function () {
    if ( arguments.length )
        throw Error("dropDatabase doesn't take arguments");
    return this._dbCommand( { dropDatabase: 1 } );
}

The mongo shell can be extended using JavaScript, so if you are really keen you could always write some functions to customise your local environment and have these functions loaded by default in your ~/.mongorc.js startup file (or the global /etc/mongorc.js for all users in a given environment).

Since I do a lot of work in the mongo shell, I find it very helpful to customise the prompt to indicate current database and other useful info such as server versions, timestamps, and node type/name I’m connected to. There are also a number of GUI tools available, which perhaps can provide better visual indication & confirmation for operations. Check out the MongoDB Tools community directory: http://mongodb-tools.com/.

Some examples of extending the shell:

A quick example of a usedb shell helper which only changes to a database if it already exists:

/* Only use a DB if it already exists */
shellHelper.usedb = function( name ) {
    var dbNames = db.getMongo().getDBNames();
    if (name && dbNames.indexOf(name) > -1) {
        db=db.getSiblingDB(name);
    } else {
        print("Sorry: " + name + " doesn't exist");
    }
}

Regards,
Stephen

A. Jalil @AJ

unread,
Sep 26, 2015, 1:12:36 AM9/26/15
to mongodb-user

Thanks Stephen, this is great stuff ! I downloaded Stennie's two-line MongoDB prompt, but I didn't see instructions how I can implement it ? I'd like to install it on my Test environment and see how it looks. Also, I was wondering if this can be used all mongo shells (mongoS, config server and mongo db)..

The same thing for the example usedb you posted above, do I just run the script from mongo shell ? or is there a way to add it to the config file so it will stay there anytime I run usdb..

Thanks.
@AJ

Stephen Steneker

unread,
Sep 26, 2015, 3:51:56 AM9/26/15
to mongodb-user

On Saturday, 26 September 2015 15:12:36 UTC+10, A. Jalil @AJ wrote:

Thanks Stephen, this is great stuff ! I downloaded Stennie's two-line MongoDB prompt, but I didn't see instructions how I can implement it ? I'd like to install it on my Test environment and see how it looks. Also, I was wondering if this can be used all mongo shells (mongoS, config server and mongo db).

Hi AJ,

There are two places where you can add JavaScript to load when a mongo shell is loaded:

  • in a .mongorc.js file in the home directory for a given user (note the leading “.” in the filename)
  • in an /etc/mongorc.js file, which will be loaded fro all users

This works with any shell session (as long as your JavaScript functions don’t make any assumptions about the type of node they are connecting to).

You can also use mongo --norc to skip loading the user-specific ~/.mongorc.js file if you suspect this is causing issues or just want out-of-the-box behaviour. The global /etc/mongorc.js is always loaded, if present.

The same thing for the example usedb you posted above, do I just run the script from mongo shell ? or is there a way to add it to the config file so it will stay there anytime I run usdb..

It’s helpful to paste/edit functions in a shell session to test, but if you want them always available you should add to either of the mongorc.js files noted above. If you have a lot of different scripts you can also save these as separate JS files and use load(...).

I should perhaps do a round up of useful shell snippets and helpers, but most folks do heavy lifting in their application language of choice. There are a number of JS utility libraries that can be handy, though .. for example Moment (for date manipulation) and Underscore or Lodash.

Regards,
Stephen

Reply all
Reply to author
Forward
0 new messages