Skip to content

add ability to directly connect to mongo with pymongo.MongoClient #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ including:
LinkML-Store is designed to work with multiple backends, giving a common abstraction layer

* [MongoDB](https://linkml.io/linkml-store/how-to/Use-MongoDB.html)
* Supports using existing MongoClient instances directly
* [DuckDB](https://linkml.io/linkml-store/tutorials/Python-Tutorial.html)
* [Solr](https://linkml.io/linkml-store/how-to/Query-Solr-using-CLI.html)
* [Neo4j](https://linkml.io/linkml-store/how-to/Use-Neo4j.html)
Expand Down
46 changes: 23 additions & 23 deletions docs/how-to/Use-MongoDB.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,17 @@
},
{
"cell_type": "markdown",
"source": [
"Next we'll attach to a MongoDB instance. this assumes you have one running already."
],
"source": "Next we'll attach to a MongoDB instance. this assumes you have one running already.\n\nYou can connect in two ways:\n1. Using a connection string\n2. Using an existing MongoClient instance\n\n### Connection using a string",
"metadata": {
"collapsed": false
},
"id": "470f1cb70bf3641b"
"id": "470f1cb70bf3641b",
"outputs": []
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"db = client.attach_database(\"mongodb://localhost:27017\", \"test\")"
],
"source": "db = client.attach_database(\"mongodb://localhost:27017\", \"test\")",
"metadata": {
"collapsed": false,
"ExecuteTime": {
Expand Down Expand Up @@ -100,22 +96,26 @@
},
"id": "cb437a6dd0f73ec8"
},
{
"cell_type": "markdown",
"source": "### Connection using an existing MongoClient\n\nIf you already have a MongoClient instance (for example, from an existing application), you can use it directly without having to create a new connection:",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [
{
"data": {
"text/plain": "'{\"handle\":\"mongodb://localhost:27017\",\"alias\":\"test\",\"schema_location\":null,\"schema_dict\":null,\"collections\":{},\"recreate_if_exists\":false,\"collection_type_slot\":null,\"searchable_slots\":null,\"ensure_referential_integrity\":false}'"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.metadata.model_dump_json()"
],
"source": "# Now let's connect using an existing MongoClient\nfrom pymongo import MongoClient\n\n# Create a MongoClient instance directly\nmongo_client = MongoClient(\"mongodb://localhost:27017/\")\n\n# Use the existing client with linkml-store\ndb2 = client.attach_mongodb_client(\n mongo_client=mongo_client,\n db_name=\"test2\",\n alias=\"direct_mongo\"\n)\n\nprint(f\"DB Handle: {db2.handle}\")\nprint(f\"DB Alias: {db2.alias}\")\nprint(f\"DB Name: {db2._db_name}\")",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"source": "This approach is particularly useful when:\n1. You already have a MongoClient configured in your application\n2. You need special authentication or connection settings\n3. You want to reuse an existing connection pool\n\nFor the rest of this tutorial, we'll continue using the first database (`db`), but all operations work the same on both connection types.",
"metadata": {}
},
{
"cell_type": "code",
"outputs": [],
"source": "db.metadata.model_dump_json()",
"metadata": {
"collapsed": false,
"ExecuteTime": {
Expand Down Expand Up @@ -594,4 +594,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
Loading
Loading