Metadata-Version: 2.4
Name: colab-mongo-magic
Version: 0.1.1
Summary: Magic commands for MongoDB in Google Colab using mongosh.
Project-URL: Homepage, https://github.com/mgarcesdev/colab-mongo-magic
Author-email: Manuel <manuegp1994@gmail.com>
Classifier: Framework :: IPython
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.7
Requires-Dist: ipython>=7.0.0
Requires-Dist: pandas>=1.0.0
Requires-Dist: pyyaml>=5.0
Requires-Dist: traitlets>=5.0.0
Description-Content-Type: text/markdown

# 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:

```python
!pip install colab-mongo-magic
```

## Usage

Once installed, load the extension in your notebook:

```python
%load_ext mongomagic
```

1. Connection Management
   Set your connection string by running the magic command without a cell body:
   [Jump to Local Server setup](#install-local-server-for-testing-purposes) to learn how to test it locally.

```python
%%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:

```mongosh
%%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!

````javascript
%%mongo %%js

db.users.insertMany([
  { name: "Ana", age: 28, role: "admin" },
  { name: "Luis", age: 35, role: "user" }
])

3. Automatic Pandas Conversion
   Enable automatic conversion of query results to a Pandas DataFrame by using the configuration magic:

```python
%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
%%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
```

2. Update and Install

```bash
%%bash
sudo apt-get update
sudo apt-get install -y mongodb-org
```

3. Configure Data Directory

```bash
%%bash
sudo mkdir -p /data/db
sudo chown -R `id -un` /data/db
```

4. Start the Server

```bash
%%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
