All your mongos in one place

Description

MongoPool is the tool that manages your connections to different
clusters, maps databases to clients and allows you to work only with
database names without worrying about creating and managing connections.

You will never have to create a MongoClient everywhere you want to
access a database again which enables you to write beautiful and
maintainable code. Using MongoPool, you will keep connection regarding
information in a single place and allows you to easily modify it when
needed.

At UberVU, we are confidently using it to manage over 25 mongo instances
to provide quality services to our customers. ## Install

Install

PyPi

$ sudo pip install mongo-pool

Manual

Usage

Basic example

All you have to do in order to get started is to build a list of
dictionaries which contains the necessary information to connect to the
clusters, instantiate MongoPool and access databases through dot
notation.

Dynamic paths

You might have databases created automatically, following a certain
naming pattern. In this case, it would be impossible to add all
databases on a cluster in dbpath. For this reason, you can pass it as a
regexp pattern. Let’s say that you save the comments in a separate
database for each month, named comments_monthyear:

Caution: This is a strong feature, but it should be used carefully.
Dbpaths will be matched in the order you put them in the configurations
list, so make sure you order them from the most particular to the most
general in order to avoid creating incorrect mappings and connect to the
wrong cluster.

Correct

Connecting to a replicaSet

MongoPool also manages connections to ReplicaSets. All you have to do is to add the name of the replica set in the configuration. Also, if you want a read_preference different from PRIMARY, you can specify it in the config.

Setting a timeout

By default, MongoClient and MongoReplicaSetClient do not have a timeout set, though sometimes it is handy. To set a timeout for you connection you can either pass it as a second argument while instantiating MongoPool or use the set_timeout method which will
recreate all connections with the new timeout and create all new
connections with the new value.

Setting it up

Along with the project we provide a sample config file to easily get started. In order to work with it, you have to launch multiple mongod instances on different ports. For this purpose, you can run the start_instances.sh script. If you don’t wish to open many mongod instances, you can change all port values in the config file to 27017 and delete label3 entry which uses a replicaSet.

# make sure that you are in the mongo-pool main directory
$ cd mongo-pool
# run the provided script or modify sample_config.yml file
$ ./start_instances.sh $ python