Mongo Repository#

MongoRepo is a repository for MongoDB data stores. MongoDB is useful if you have unstructured data or you wish to store data in JSON format.

from redbird.repos import MongoRepo
repo = MongoRepo(uri="mongodb://USERNAME:PASSWORD@localhost:27017", database="my_db", collection="my_items")

Alternatively, you may pass the client:

from redbird.repos import MongoRepo
from pymongo import MongoClient
repo = MongoRepo(client=MongoClient("mongodb://USERNAME:PASSWORD@localhost:27017"), database="my_db", collection="my_items")

With Pydantic model:

from redbird.repos import MongoRepo

class MyItem(BaseModel):
    id: str
    name: str
    age: int

repo = MongoRepo(
    uri="mongodb://USERNAME:PASSWORD@localhost:27017",
    database="my_db",
    collection="my_items",
    model=MyItem
)

Usage#

Now you may use the repository the same way as any other repository. Please see:

Class#

class redbird.repos.MongoRepo(*args, uri=None, client=None, session=None, model=<class 'dict'>, id_field=None, query_model=<class 'redbird.base.BasicQuery'>, errors_query='raise', field_access='infer', ordered=True, database=None, collection=None, default_id_field='_id')#

MongoDB Repository

Parameters
  • uri (str, optional) – Connection string to the database. Pass uri, client or session.

  • client (mongodb.MongoClient, optional) – MongoDB client for the connection. Pass uri, client or session.

  • model (Type) – Class of an item in the repository. Commonly dict or subclass of Pydantic BaseModel. By default dict

  • id_field (str, optional) – Attribute or key that identifies each item in the repository.

  • field_access ({'attr', 'key'}, optional) – How to access a field in an item. Either by attribute (‘attr’) or key (‘item’). By default guessed from the model.

  • query (Type, optional) – Query model of the repository.

  • errors_query ({'raise', 'warn', 'discard'}) – Whether to raise an exception, warn or discard the item in case of validation error in converting data to the item model from the repository. By default raise

  • session (Session, Any) – A MongoDB session object that should have at least client attribute. Pass uri, client or session.

Examples

repo = MongoRepo(uri="mongodb://localhost:27017/mydb?authSource=admin", collection="mycol")
repo = MongoRepo(uri="mongodb://localhost:27017", database="mydb", collection="mycol")
from pymongo import MongoClient
repo = MongoRepo(client=MongoClient("mongodb://localhost:27017"))
insert(item)#

Insert item to the repository

Parameters

item (instance of model) – Item to insert to the repository

Examples

repo.insert(Item(id="a", color="red"))
filter_by(**kwargs)#

Filter the repository

Parameters

**kwargs (dict) – Query which is used to conduct furher operation.

Examples

repo.filter_by(color="red")
update(item)#

Update item in the repository

Parameters

item (instance of model) – Item to update in the repository

Examples

repo.update(Item(id="a", color="red"))
delete(item)#

Delete item from the repository

Parameters

item (instance of model) – Item to delete from the repository

Examples

repo.delete(Item(id="a", color="red"))