Magic commands for MongoDB in Google Colab using mongosh.
Project description
Mongo Magic for Jupyter
A lightweight Jupyter/Colab magic extension to execute mongosh commands directly in notebook cells.
Installation
You can install this package via pip:
!pip install colab-mongo-magic
Usage
Once installed, load the extension in your notebook:
%load_ext mongomagic
- Connection Management Set your connection string by running the magic command without a cell body: Jump to Local Server setup to learn how to test it locally.
%%mongo mongodb://localhost:27017/my_database
To check the current connection, run %%mongo without arguments. 2. Execute Queries Run standard mongosh commands in a cell:
%%mongo
db.users.find().limit(5)
💡 Tip: Enable Syntax Highlighting
Since mongosh is based on JavaScript, you can easily trick the Colab/Jupyter editor into providing JavaScript syntax highlighting. Just add %%js right after the cell magic. The extension will safely ignore it during execution, but your code will look much better!
%%mongo %%js
db.users.insertMany([
{ name: "Ana", age: 28, role: "admin" },
{ name: "Luis", age: 35, role: "user" }
])
- Automatic Pandas Conversion Enable automatic conversion of query results to a Pandas DataFrame by using the configuration magic:
%config MongoMagic.autopandas = True
Install Local Server for testing purposes
To run a full MongoDB server locally (e.g., in a Linux environment or Google Colab), follow these steps:
- Add MongoDB Repository
%%bash
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
- Update and Install
%%bash
sudo apt-get update
sudo apt-get install -y mongodb-org
- Configure Data Directory
%%bash
sudo mkdir -p /data/db
sudo chown -R `id -un` /data/db
- Start the Server
%%bash
mongod --fork --logpath /var/log/mongodb/mongod.log --dbpath /data/db
Features
- Auto-installation: Automatically installs the mongosh binary when running in Google Colab.
- Seamless Integration: Uses mongosh native engine to ensure full compatibility with MongoDB syntax.
- Pandas Support: Cleanly transforms JSON output from MongoDB into ready-to-use DataFrames.
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file colab_mongo_magic-0.1.2.tar.gz.
File metadata
- Download URL: colab_mongo_magic-0.1.2.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
572f915aa4c3edbca34aa11fde9bf864861ab4452ab131095fa65096114d0489
|
|
| MD5 |
5e9270b4aae8462149812f5bb1e0df4e
|
|
| BLAKE2b-256 |
79e0e77cdc6852ed7b9355587016a4de780183191e75ddac3e7810518fbc46a6
|
File details
Details for the file colab_mongo_magic-0.1.2-py3-none-any.whl.
File metadata
- Download URL: colab_mongo_magic-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f079b9b6d3428971d2ddfbcfcdebe7a46f877c600aad3074ef08d974ba4986b3
|
|
| MD5 |
26501467dd5d46a7793095a4c5a300c5
|
|
| BLAKE2b-256 |
7ee4de51ded0a2fd1362ca5ba51617d76c8ba156dbfdd470e125530746cb31f6
|