Connecting to MongoDBΒΆ

To connect to a running instance of mongod, use the connect() function. The first argument is the name of the database to connect to. If the database does not exist, it will be created. If the database requires authentication, username and password arguments may be provided:

from mongoengine import connect
connect('project1', username='webapp', password='pwd123')

By default, MongoEngine assumes that the mongod instance is running on localhost on port 27017. If MongoDB is running elsewhere, you may provide host and port arguments to connect():

connect('project1', host='192.168.1.35', port=12345)

Previous topic

Installing MongoEngine

Next topic

Defining documents

This Page