Python MongoDB programming

MongoDB through outgoing connections - inserting, finding, updating and aggregating documents with pymongo.

Create an outgoing MongoDB connection in Dashboard and everything that MongoDB offers will be available to your services via the underlying pymongo library, as in the examples below.

Creating a connection

In Dashboard, go to Connections -> NoSQL -> MongoDB and click Create a new outgoing MongoDB connection.

Fill in the form:

  • Name - any name of your choice
  • Server list - one host:port entry per line, e.g. a single localhost:27017 line or several lines when connecting to a replica set
  • Username and password - credentials to authenticate with, the auth source defaults to admin
  • Replica set - optional, the name of the replica set to require

The TLS section lets you connect to servers that require encrypted connections - turn the TLS checkbox on and point the CA certificates file to the PEM certificate of the authority that signed the server's certificate. For mutual TLS, add a combined client certificate and private key PEM file too.

After the connection is created, click Ping to confirm that Zato can reach the server.

Inserting documents

In your services, look up the connection by name with self.mongodb - what you get back is a pymongo client, so databases and collections are accessed the way pymongo does it.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):

        # Get a client by the connection's name
        conn = self.mongodb['My MongoDB Connection']

        # Pick a database and a collection
        db = conn['my_database']
        orders = db['orders']

        # Insert a document
        result = orders.insert_one({'order_id': 123, 'status': 'ready'})

        self.logger.info('Inserted: %s', result.inserted_id)

Finding documents

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.mongodb['My MongoDB Connection']
        orders = conn['my_database']['orders']

        # Find one document
        order = orders.find_one({'order_id': 123})
        self.logger.info('Order: %s', order)

        # Find all documents matching a query
        for item in orders.find({'status': 'ready'}):
            self.logger.info('Ready: %s', item)

Updating and deleting

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.mongodb['My MongoDB Connection']
        orders = conn['my_database']['orders']

        # Update a document
        orders.update_one({'order_id': 123}, {'$set': {'status': 'shipped'}})

        # Delete a document
        orders.delete_one({'order_id': 123})

Aggregations

The full aggregation pipeline is available too.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.mongodb['My MongoDB Connection']
        orders = conn['my_database']['orders']

        # Count orders per status
        pipeline = [
            {'$group': {'_id': '$status', 'count': {'$sum': 1}}},
            {'$sort': {'count': -1}},
        ]

        for row in orders.aggregate(pipeline):
            self.logger.info('%s: %s', row['_id'], row['count'])

Pinging from a service

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        conn = self.mongodb['My MongoDB Connection']

        # The same command that the Dashboard's Ping link runs
        conn.admin.command('ping')

        self.logger.info('MongoDB is reachable')

Testing with a local server

To test your connections without an existing MongoDB installation, one command starts a complete server in Docker:

docker run -d --rm --name zato-mongodb -e MONGO_INITDB_ROOT_USERNAME=zato -e MONGO_INITDB_ROOT_PASSWORD=zato -p 27017:27017 mongo:8

In Dashboard, create an outgoing MongoDB connection with localhost:27017 in the server list, username and password both set to zato, and click Ping to confirm that Zato can reach the server.

When you are done, the container removes itself on stop:

docker stop zato-mongodb

Learn more