Welcome to mongomock_mate Documentation

Use mongomock_mate:

Just import mongomock_mate at begin of your script. It use monkey patch:

import mongomock_mate


Data Persistence: dump and load data

import mongomock_mate

# dump
db = client["test"]
c_user = db["user"]

c_user.insert([{"_id": 1, "name": "Alice"}, {"_id": 2, "name": "Bob"}])


# load
db = client["test"]

# other params
def dump_db(self, file,
    Dump :class:`mongomock.database.Database` to a local file. Only support
    ``*.json`` or ``*.gz`` (compressed json file)

    :param file: file path.
    :param pretty: bool, toggle on jsonize into pretty format.
    :param overwrite: bool, allow overwrite to existing file.
    :param verbose: bool, toggle on log.

def load_db(self, file, check_dbname=True, verbose=True):
    Load :class:`mongomock.database.Database` from a local file.

    :param file: file path.
    :param check_dbname: bool, if True, the dbname has to be matched.
    :param verbose: bool, toggle on log.

Working with mongoengine ORM:


At mongomock==3.10.0, insert operation doesn’t work with latest mongoengine==0.15.3, because the implementation of WriteConcern is not correct in mongomock.

Github Issue.

import mongomock_mate
from mongoengine import connect, Document, fields

connect('mongoenginetest', host='mongomock://localhost')

class User(Document):
    _id = fields.IntField(primary_key=True)
    name = fields.StringField()

User.objects.insert(User(_id=1, name="Alice"))


mongomock_mate is released on PyPI, so all you need is:

$ pip install mongomock_mate

To upgrade to latest version:

$ pip install --upgrade mongomock_mate

About the Author

(\ (\
( -.-)o    I am a lovely Rabbit!

Sanhe Hu is a very active Python Developer Since 2010. Now working at Whiskerlabs as a Data Scientist. Research area includes Machine Learning, Big Data Infrastructure, Block Chain, Business Intelligent, Open Cloud, Distribute System. Love photography, vocal, outdoor, arts, game, and also the best Python.