Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a different database for each collection in MongoDB 2.2

Tags:

mongodb

MongoDB 2.2 has a write lock per database as opposed to a global write lock on the server in previous versions. So would it be ok if i store each collection in a separate database to effectively have a write lock per collection.(This will make it look like MyISAM's table level locking). Is this approach faulty?

like image 771
lovesh Avatar asked Jul 26 '12 15:07

lovesh


People also ask

Can you have multiple databases in MongoDB?

A single MongoDB server can have multiple databases and a single MongoDB database can have multiple collections. You can use MongoDB Shell or MongoDB Compass to create a new database. MongoDB provides the use <database-name command to connect with the database.

How many databases can be created in MongoDB?

MongoDB supports no more than 100 levels of nesting for BSON documents. Each object or array adds a level.

How do databases and collections get created in MongoDB?

In MongoDB, a new collection is created when we add one or more documents to it. We can insert documents in the collection using the following methods: Inserting only one document in the collection.

What is the difference between database and collection in MongoDB?

MongoDB stores data records as documents (specifically BSON documents) which are gathered together in collections. A database stores one or more collections of documents.


2 Answers

There's a key limitation to the locking and that is the local database. That database includes a the oplog collection which is used for replication.

If you're running in production, you should be running with Replica Sets. If you're running with Replica Sets, you need to be aware of the write lock effect on that database.

Breaking out your 10 collections into 10 DBs is useless if they all block waiting for the oplog.

Before taking a large step to re-write, please ensure that the oplog will not cause issues.

Also, be aware that MongoDB implements DB-level security. If you're using any security features, you are now creating more DBs to secure.

like image 116
Gates VP Avatar answered Sep 29 '22 00:09

Gates VP


Yes that will work, 10gen actually offers this as an option in their talks on locking.

I probably isolate every collection, though. Most databases seem to have 2-5 high activity collections. For the sake of simplicity it's probably better to keep the low activity collections grouped in one DB and put high activity collections in their own databases.

like image 45
MrKurt Avatar answered Sep 29 '22 01:09

MrKurt