- class MongoDbContainer(image: str = 'mongo:latest', port: int = 27017, username: str | None = None, password: str | None = None, dbname: str | None = None, **kwargs)¶
Mongo document-based database container.
Example
>>> from testcontainers.community.mongodb import MongoDbContainer >>> with MongoDbContainer("mongo:7.0.7") as mongo: ... db = mongo.get_connection_client().test ... # Insert a database entry ... result = db.restaurants.insert_one( ... { ... "name": "Vella", ... "cuisine": "Italian", ... "restaurant_id": "123456" ... } ... ) ... # Find the restaurant document ... result = db.restaurants.find_one({"name": "Vella"}) ... result["restaurant_id"] '123456'
- class MongoDBAtlasLocalContainer(image: str = 'mongodb/mongodb-atlas-local:latest', port: int = 27017, username: str | None = None, password: str | None = None, dbname: str | None = None, **kwargs)¶
MongoDB Atlas Local document-based database container.
This is the local version of the Mongo Atlas service. It includes Mongo DB and Mongo Atlas Search services .. rubric:: Example
>>> from testcontainers.community.mongodb import MongoDBAtlasLocalContainer >>> import time >>> with MongoDBAtlasLocalContainer("mongodb/mongodb-atlas-local:8.0.13") as mongo: ... db = mongo.get_connection_client().test ... # Insert a database entry ... result = db.restaurants.insert_one( ... { ... "name": "Vella", ... "cuisine": "Italian", ... "restaurant_id": "123456" ... } ... ) ... # add an index ... _ = db.restaurants.create_search_index( ... { ... "definition": { ... "mappings": { ... "dynamic": True ... } ... }, ... "name": "default" ... } ... ) ... # wait for the index to be created ... time.sleep(1) ... ... # Find the restaurant document ... result = db.restaurants.aggregate([{ ... "$search": { ... "index": "default", ... "text": { ... "query": "Vella", ... "path": "name" ... } ... } ... }]).next() ... result["restaurant_id"] '123456'