Skip to main content

Mongo Magic for Google Colab

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
  1. 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" }
])
  1. 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:

  1. 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
  1. Update and Install
%%bash
sudo apt-get update
sudo apt-get install -y mongodb-org
  1. Configure Data Directory
%%bash
sudo mkdir -p /data/db
sudo chown -R `id -un` /data/db
  1. 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

Metadata

Release files for colab-mongo-magic 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for colab-mongo-magic 0.1.3
File Size Uploaded
colab_mongo_magic-0.1.3.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for colab-mongo-magic 0.1.3
File Interpreter ABI Platform
colab_mongo_magic-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / colab_mongo_magic-0.1.3.tar.gz

Download URL colab_mongo_magic-0.1.3.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
59271d69a6a65a798420dd4f94a951bfebc2bfed5c40bbb19a0ce9ef98fe749e
BLAKE2b-256 checksum
How to use checksums
e4c407fc0abda05534b5972701f16f2b529d817fac75766a484799167606f4ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.8

Release files / colab_mongo_magic-0.1.3-py3-none-any.whl

Download URL colab_mongo_magic-0.1.3-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb2061a1b4476e8acca8e296096b8e06b933b05bfc8f969c84d0b0ab34019f10
BLAKE2b-256 checksum
How to use checksums
d03d186f55368e79cca46389c2c222bc2dbf8d61fe7e2e443018e346ba5bcf59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.8

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page