Skip to main content

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
  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)
  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

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

colab_mongo_magic-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colab_mongo_magic-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file colab_mongo_magic-0.1.0.tar.gz.

File metadata

  • Download URL: colab_mongo_magic-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for colab_mongo_magic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 176b43fe28e076a1350bd531c63b1d65b40c928f3f151824d29c115304bde0be
MD5 70c172b3ef2f5103fe6b2df5c2bb8911
BLAKE2b-256 84dacb95e1187849bc7300518f8bbe7ae39723b4931572b50dfdaa1d089827cb

See more details on using hashes here.

File details

Details for the file colab_mongo_magic-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for colab_mongo_magic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43c2df7bb72ca94d076c12247e3f42afdd77ba1b11cb05f49c0a986b9384738b
MD5 a03e616f39729cd80d1de82c330ebf7b
BLAKE2b-256 c091eb211e7c881dda1d31adfe7f3e22f08f30cbb013d56b5d98258c7f35394e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page