monggregate


Namemonggregate JSON
Version 0.21.0 PyPI version JSON
download
home_pageNone
SummaryMongoDB aggregation pipelines made easy. Joins, grouping, counting and much more...
upload_time2024-04-17 19:58:23
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseThe MIT License (MIT) Copyright © 2022 Vianney Mixtur Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords nosql mongo aggregation pymongo pandas pydantic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## **Overview**

Monggregate is a library that aims at simplifying usage of MongoDB aggregation pipelines in Python.
It is based on MongoDB official Python driver, pymongo and on [pydantic](https://pydantic-docs.helpmanual.io/).

### Features

- Provides an Object Oriented Programming (OOP) interface to the aggregation pipeline.
- Allows you to focus on your requirements rather than MongoDB syntax.
- Integrates all the MongoDB documentation and allows you to quickly refer to it without having to navigate to the website.
- Enables autocompletion on the various MongoDB features.
- Offers a pandas-style way to chain operations on data.

## **Requirements**

This package requires python > 3.10, pydantic > 1.8.0

## **Installation**

The repo is now available on PyPI:

```shell
pip install monggregate
```


## **Usage**

The below examples reference the MongoDB sample_mflix database

### Basic Pipeline usage

```python
import os

from dotenv import load_dotenv 
import pymongo
from monggregate import Pipeline, S

# Creating connexion string securely
# You need to create a .env file with your password
load_dotenv(verbose=True)
PWD = os.environ["MONGODB_PASSWORD"] 

MONGODB_URI = f"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority"

# Connect to your MongoDB cluster:
client = pymongo.MongoClient(MONGODB_URI)

# Get a reference to the "sample_mflix" database:
db = client["sample_mflix"]

# Creating the pipeline
pipeline = Pipeline()

# The below pipeline will return the most recent movie with the title "A Star is Born"
pipeline.match(
    title="A Star Is Born"
).sort(
    by="year"
).limit(
    value=1
)

# Executing the pipeline
curosr = db["movies"].aggregate(pipeline.export())

# Printing the results
results = list(curosr)
print(results)
```



### Advanced Usage, with MongoDB Operators


```python
import os

from dotenv import load_dotenv 
import pymongo
from monggregate import Pipeline, S


# Creating connexion string securely
load_dotenv(verbose=True)
PWD = os.environ["MONGODB_PASSWORD"]
MONGODB_URI = f"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority"


# Connect to your MongoDB cluster:
client = pymongo.MongoClient(MONGODB_URI)

# Get a reference to the "sample_mflix" database:
db = client["sample_mflix"]


# Creating the pipeline
pipeline = Pipeline()
pipeline.match(
    year=S.type_("number") # Filtering out documents where the year field is not a number
).group(
    by="year",
    query = {
        "movie_count":S.sum(1), # Aggregating the movies per year
        "movie_titles":S.push("$title")
    }
).sort(
    by="_id",
    descending=True
).limit(10)

# Executing the pipeline
cursor = db["movies"].aggregate(pipeline.export())

# Printing the results
results = list(cursor)
print(results)

```

### Even More Advanced Usage with Expressions

```python
import os

from dotenv import load_dotenv 
import pymongo
from monggregate import Pipeline, S, Expression

# Creating connexion string securely
load_dotenv(verbose=True)
PWD = os.environ["MONGODB_PASSWORD"]
MONGODB_URI = f"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority"


# Connect to your MongoDB cluster:
client = pymongo.MongoClient(MONGODB_URI)

# Get a reference to the "sample_mflix" database:
db = client["sample_mflix"]

# Using expressions
comments_count = Expression.field("comments").size()


# Creating the pipeline
pipeline = Pipeline()
pipeline.lookup(
    right="comments",
    right_on="movie_id",
    left_on="_id",
    name="comments"
).add_fields(
    comments_count=comments_count
).match(
    expression=comments_count>2
).limit(1)

# Executing the pipeline
cursor = db["movies"].aggregate(pipeline.export())

# Printing the results
results = list(cursor)
print(results)
```

## **Going Further**

* Check out the [full documentation](https://vianneymi.github.io/monggregate/) for more examples.
* Check out this [medium article](https://medium.com/@vianney.mixtur_39698/mongo-db-aggregations-pipelines-made-easy-with-monggregate-680b322167d2).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "monggregate",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "nosql, mongo, aggregation, pymongo, pandas, pydantic",
    "author": null,
    "author_email": "Vianney Mixtur <vianney.mixtur@outlook.fr>",
    "download_url": "https://files.pythonhosted.org/packages/c1/97/13f59dbcba227eea1b10f487a3400c005fda859e56b622255f30dac92eb1/monggregate-0.21.0.tar.gz",
    "platform": null,
    "description": "## **Overview**\n\nMonggregate is a library that aims at simplifying usage of MongoDB aggregation pipelines in Python.\nIt is based on MongoDB official Python driver, pymongo and on [pydantic](https://pydantic-docs.helpmanual.io/).\n\n### Features\n\n- Provides an Object Oriented Programming (OOP) interface to the aggregation pipeline.\n- Allows you to focus on your requirements rather than MongoDB syntax.\n- Integrates all the MongoDB documentation and allows you to quickly refer to it without having to navigate to the website.\n- Enables autocompletion on the various MongoDB features.\n- Offers a pandas-style way to chain operations on data.\n\n## **Requirements**\n\nThis package requires python > 3.10, pydantic > 1.8.0\n\n## **Installation**\n\nThe repo is now available on PyPI:\n\n```shell\npip install monggregate\n```\n\n\n## **Usage**\n\nThe below examples reference the MongoDB sample_mflix database\n\n### Basic Pipeline usage\n\n```python\nimport os\n\nfrom dotenv import load_dotenv \nimport pymongo\nfrom monggregate import Pipeline, S\n\n# Creating connexion string securely\n# You need to create a .env file with your password\nload_dotenv(verbose=True)\nPWD = os.environ[\"MONGODB_PASSWORD\"] \n\nMONGODB_URI = f\"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority\"\n\n# Connect to your MongoDB cluster:\nclient = pymongo.MongoClient(MONGODB_URI)\n\n# Get a reference to the \"sample_mflix\" database:\ndb = client[\"sample_mflix\"]\n\n# Creating the pipeline\npipeline = Pipeline()\n\n# The below pipeline will return the most recent movie with the title \"A Star is Born\"\npipeline.match(\n    title=\"A Star Is Born\"\n).sort(\n    by=\"year\"\n).limit(\n    value=1\n)\n\n# Executing the pipeline\ncurosr = db[\"movies\"].aggregate(pipeline.export())\n\n# Printing the results\nresults = list(curosr)\nprint(results)\n```\n\n\n\n### Advanced Usage, with MongoDB Operators\n\n\n```python\nimport os\n\nfrom dotenv import load_dotenv \nimport pymongo\nfrom monggregate import Pipeline, S\n\n\n# Creating connexion string securely\nload_dotenv(verbose=True)\nPWD = os.environ[\"MONGODB_PASSWORD\"]\nMONGODB_URI = f\"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority\"\n\n\n# Connect to your MongoDB cluster:\nclient = pymongo.MongoClient(MONGODB_URI)\n\n# Get a reference to the \"sample_mflix\" database:\ndb = client[\"sample_mflix\"]\n\n\n# Creating the pipeline\npipeline = Pipeline()\npipeline.match(\n    year=S.type_(\"number\") # Filtering out documents where the year field is not a number\n).group(\n    by=\"year\",\n    query = {\n        \"movie_count\":S.sum(1), # Aggregating the movies per year\n        \"movie_titles\":S.push(\"$title\")\n    }\n).sort(\n    by=\"_id\",\n    descending=True\n).limit(10)\n\n# Executing the pipeline\ncursor = db[\"movies\"].aggregate(pipeline.export())\n\n# Printing the results\nresults = list(cursor)\nprint(results)\n\n```\n\n### Even More Advanced Usage with Expressions\n\n```python\nimport os\n\nfrom dotenv import load_dotenv \nimport pymongo\nfrom monggregate import Pipeline, S, Expression\n\n# Creating connexion string securely\nload_dotenv(verbose=True)\nPWD = os.environ[\"MONGODB_PASSWORD\"]\nMONGODB_URI = f\"mongodb+srv://dev:{PWD}@myserver.xciie.mongodb.net/?retryWrites=true&w=majority\"\n\n\n# Connect to your MongoDB cluster:\nclient = pymongo.MongoClient(MONGODB_URI)\n\n# Get a reference to the \"sample_mflix\" database:\ndb = client[\"sample_mflix\"]\n\n# Using expressions\ncomments_count = Expression.field(\"comments\").size()\n\n\n# Creating the pipeline\npipeline = Pipeline()\npipeline.lookup(\n    right=\"comments\",\n    right_on=\"movie_id\",\n    left_on=\"_id\",\n    name=\"comments\"\n).add_fields(\n    comments_count=comments_count\n).match(\n    expression=comments_count>2\n).limit(1)\n\n# Executing the pipeline\ncursor = db[\"movies\"].aggregate(pipeline.export())\n\n# Printing the results\nresults = list(cursor)\nprint(results)\n```\n\n## **Going Further**\n\n* Check out the [full documentation](https://vianneymi.github.io/monggregate/) for more examples.\n* Check out this [medium article](https://medium.com/@vianney.mixtur_39698/mongo-db-aggregations-pipelines-made-easy-with-monggregate-680b322167d2).\n",
    "bugtrack_url": null,
    "license": "The MIT License (MIT) Copyright \u00a9 2022 Vianney Mixtur  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \u201cSoftware\u201d), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \u201cAS IS\u201d, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "MongoDB aggregation pipelines made easy. Joins, grouping, counting and much more...",
    "version": "0.21.0",
    "project_urls": {
        "Homepage": "https://github.com/VianneyMI/monggregate",
        "documentation": "https://vianneymi.github.io/monggregate/"
    },
    "split_keywords": [
        "nosql",
        " mongo",
        " aggregation",
        " pymongo",
        " pandas",
        " pydantic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "475fe5962f3239ddb7a221375f768ddb5993533feec161d44f56e4cb7b865859",
                "md5": "25f5778f1c79821898625be5167f73ab",
                "sha256": "61622d2af60558c313daba8076c299d8e892a0825c6ad043ad65e771b0f20065"
            },
            "downloads": -1,
            "filename": "monggregate-0.21.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "25f5778f1c79821898625be5167f73ab",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 169991,
            "upload_time": "2024-04-17T19:58:20",
            "upload_time_iso_8601": "2024-04-17T19:58:20.505934Z",
            "url": "https://files.pythonhosted.org/packages/47/5f/e5962f3239ddb7a221375f768ddb5993533feec161d44f56e4cb7b865859/monggregate-0.21.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c19713f59dbcba227eea1b10f487a3400c005fda859e56b622255f30dac92eb1",
                "md5": "d6f5b915a7c8caf1feef2e547ea64ab8",
                "sha256": "84abdc28b211f609b0c2c8b6c76d6753b96269c274b178aa3202a61b086eccfb"
            },
            "downloads": -1,
            "filename": "monggregate-0.21.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d6f5b915a7c8caf1feef2e547ea64ab8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 118414,
            "upload_time": "2024-04-17T19:58:23",
            "upload_time_iso_8601": "2024-04-17T19:58:23.084221Z",
            "url": "https://files.pythonhosted.org/packages/c1/97/13f59dbcba227eea1b10f487a3400c005fda859e56b622255f30dac92eb1/monggregate-0.21.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-17 19:58:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "VianneyMI",
    "github_project": "monggregate",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "monggregate"
}
        
Elapsed time: 0.22552s