Connecting

Simple Connection

MotorEngine supports connecting to the database using a myriad of options via the connect method.

motorengine.connection.connect(db, host="localhost", port=27017, io_loop=io_loop)

Connect to the database specified by the ‘db’ argument.

Connection settings may be provided here as well if the database is not running on the default port on localhost. If authentication is needed, provide username and password arguments as well.

Multiple databases are supported by using aliases. Provide a separate alias to connect to a different instance of mongod.

Extra keyword-arguments are passed to Motor when connecting to the database.

from motorengine import connect

# instantiate tornado server and apps so we get io_loop instance

io_loop = tornado.ioloop.IOLoop.instance()

# you only need to keep track of the DB instance if you connect to multiple databases.
connect("connecting-test", host="localhost", port=27017, io_loop=io_loop)

Replica Sets

motorengine.connection.connect(db, host="localhost:27017, localhost:27018", replicaSet="myRs", io_loop=self.io_loop)

Connect to the database specified by the ‘db’ argument.

Connection settings may be provided here as well if the database is not running on the default port on localhost. If authentication is needed, provide username and password arguments as well.

Multiple databases are supported by using aliases. Provide a separate alias to connect to a different instance of mongod.

Extra keyword-arguments are passed to Motor when connecting to the database.

from motorengine import connect

# instantiate tornado server and apps so we get io_loop instance

io_loop = tornado.ioloop.IOLoop.instance()
connect("connecting-test", host="localhost:27017,localhost:27018", replicaSet="myRs", io_loop=io_loop)

The major difference here is that instead of passing a single host, you need to pass all the host:port entries, comma-separated in the host parameter.

You also need to specify the name of the Replica Set in the replicaSet parameter (the naming is not pythonic to conform to Motor and thus to pyMongo).

Multiple Databases

motorengine.connection.connect(db, alias="db1", host="localhost", port=27017, io_loop=io_loop)

Connect to the database specified by the ‘db’ argument.

Connection settings may be provided here as well if the database is not running on the default port on localhost. If authentication is needed, provide username and password arguments as well.

Multiple databases are supported by using aliases. Provide a separate alias to connect to a different instance of mongod.

Extra keyword-arguments are passed to Motor when connecting to the database.

Connecting to multiple databases is as simple as specifying a different alias to each connection.

Let’s say you need to connect to an users and a posts databases:

from motorengine import connect

# instantiate tornado server and apps so we get io_loop instance

io_loop = tornado.ioloop.IOLoop.instance()

connect("posts", host="localhost", port=27017, io_loop=io_loop)                 # the posts database is the default
connect("users", alias="users", host="localhost", port=27017, io_loop=io_loop)  # the users database uses an alias

# now when querying for users we'll just specify the alias we want to use
User.objects.find_all(alias="users")