linkml_store.api.stores.mongodb.mongodb_database module

class MongoDBDatabase(handle=None, **kwargs)[source]

Bases: Database

An adapter for MongoDB databases.

The LinkML-Store Database abstraction combines mongodb Client and Database.

collection_class

alias of MongoDBCollection

__init__(handle=None, **kwargs)[source]
property native_client: MongoClient
property native_db: Database
commit(**kwargs)[source]

Commit pending changes to the database.

Parameters:

kwargs

Returns:

close(**kwargs)[source]

Close the database.

Parameters:

kwargs

Returns:

drop(**kwargs)[source]

Drop the database and all collections.

>>> from linkml_store.api.client import Client
>>> client = Client()
>>> path = Path("/tmp/test.db")
>>> path.parent.mkdir(exist_ok=True, parents=True)
>>> db = client.attach_database(f"duckdb:///{path}")
>>> db.store({"persons": [{"id": "P1", "name": "John", "age_in_years": 30}]})
>>> coll = db.get_collection("persons")
>>> coll.find({}).num_rows
1
>>> db.drop()
>>> db = client.attach_database("duckdb:///tmp/test.db", alias="test")
>>> coll = db.get_collection("persons")
>>> coll.find({}).num_rows
0
Parameters:

kwargs – additional arguments

query(query, **kwargs)[source]

Run a query against the database.

Examples

>>> from linkml_store.api.client import Client
>>> from linkml_store.api.queries import Query
>>> client = Client()
>>> db = client.attach_database("duckdb", alias="test")
>>> collection = db.create_collection("Person")
>>> collection.insert([{"id": "P1", "name": "John"}, {"id": "P2", "name": "Alice"}])
>>> query = Query(from_table="Person", where_clause={"name": "John"})
>>> result = db.query(query)
>>> len(result.rows)
1
>>> result.rows[0]["id"]
'P1'
type query:

Query

param query:

type kwargs:

param kwargs:

rtype:

QueryResult

return:

init_collections()[source]

Initialize collections.

TODO: Not typically called directly: consider making this private :return:

export_database(location, target_format=None, **kwargs)[source]

Export a database to a file or location.

>>> from linkml_store.api.client import Client
>>> client = Client()
>>> db = client.attach_database("duckdb", alias="test")
>>> db.import_database("tests/input/iris.csv", Format.CSV, collection_name="iris")
>>> db.export_database("/tmp/iris.yaml", Format.YAML)
Parameters:
  • location (str) – location of the file

  • target_format (Union[str, Format, None]) – target format

  • kwargs – additional arguments

import_database(location, source_format=None, **kwargs)[source]

Import a database from a file or location.

Parameters:
  • location (str) – location of the file

  • source_format (Optional[str]) – source format

  • kwargs – additional arguments