有没有Python库可以生成mongodb聚合管道?[关闭]

xdnvmnnf  于 2023-11-17  发布在  Go
关注(0)|答案(1)|浏览(122)

**已关闭。**此问题正在寻求有关书籍、工具、软件库等内容的建议。它不符合Stack Overflow guidelines。当前不接受答案。

我们不允许提出问题来寻求对图书、工具、软件库等的推荐。您可以编辑问题,以便用事实和引文来回答。
10天前关闭。
Improve this question
编写MongoDB聚合管道在某种程度上很难并且容易出错,有没有任何库可以让我这样做

agg_gen = AggGenerator(Document)
pipelines = agg_gen.match(...).unwind(...).project(...).out(...)

字符串
并且它正确地生成正确的流水线?
我试着为自己写一个非常轻的库,但它没有扩展太多...

ukdjmx9f

ukdjmx9f1#

你可以使用Aggify包,我自己写的这个包,很多查询变得更容易了

什么是Aggify?

Aggify是一个Python库,旨在生成MongoDB聚合管道,特别是与Mongoengine平滑集成。它旨在通过组织和直观的界面使构建复杂MongoDB查询和聚合的过程更加简单。该库允许您以编程方式构建聚合管道,封装过滤,投影,分组,以及其他操作,以获得更好的代码可维护性。

如何使用?

要使用Aggify,您可以通过pip install aggify通过pip安装它。存储库中的代码片段演示了如何轻松构建管道,利用库的功能构建查询,执行查找,分组数据,注解,编校信息,并将结果投影到指定的输出集合。
Github:https://github.com/Aggify/aggify
Pypi:https://pypi.org/project/aggify/

示例代码

以下是aggify repo的一个例子:

from mongoengine import Document, fields

class AccountDocument(Document):
    username = fields.StringField()
    display_name = fields.StringField()
    phone = fields.StringField()
    is_verified = fields.BooleanField()
    disabled_at = fields.LongField()
    deleted_at = fields.LongField()
    banned_at = fields.LongField()

class FollowAccountEdge(Document):
    start = fields.ReferenceField("AccountDocument")
    end = fields.ReferenceField("AccountDocument")
    accepted = fields.BooleanField()
    meta = {
        "collection": "edge.follow.account",
    }

class BlockEdge(Document):
    start = fields.ObjectIdField()
    end = fields.ObjectIdField()
    meta = {
        "collection": "edge.block",
    }

from models import *
from aggify import Aggify, F, Q
from bson import ObjectId

aggify = Aggify(AccountDocument)

pipelines = list(
    (
        aggify.filter(
            phone__in=[],
            id__ne=ObjectId(),
            disabled_at=None,
            banned_at=None,
            deleted_at=None,
            network_id=ObjectId(),
        )
        .lookup(
            FollowAccountEdge,
            let=["id"],
            query=[Q(start__exact=ObjectId()) & Q(end__exact="id")],
            as_name="followed",
        )
        .lookup(
            BlockEdge,
            let=["id"],
            as_name="blocked",
            query=[
                (Q(start__exact=ObjectId()) & Q(end__exact="id"))
                | (Q(end__exact=ObjectId()) & Q(start__exact="id"))
            ],
        )
        .filter(followed=[], blocked=[])
        .group("username")
        .annotate(annotate_name="phone", accumulator="first", f=F("phone") + 10)
        .redact(
            value1="phone",
            condition="==",
            value2="132",
            then_value="keep",
            else_value="prune",
        )
        .project(username=0)[5:10]
        .out(coll="account")
    )
)

字符串
以下查询生成此管道:

[
    {
        "$match": {
            "phone": {"$in": []},
            "_id": {"$ne": ObjectId("65486eae04cce43c5469e0f1")},
            "disabled_at": None,
            "banned_at": None,
            "deleted_at": None,
            "network_id": ObjectId("65486eae04cce43c5469e0f2"),
        }
    },
    {
        "$lookup": {
            "from": "edge.follow.account",
            "let": {"id": "$_id"},
            "pipeline": [
                {
                    "$match": {
                        "$expr": {
                            "$and": [
                                {
                                    "$eq": [
                                        "$start",
                                        ObjectId("65486eae04cce43c5469e0f3"),
                                    ]
                                },
                                {"$eq": ["$end", "$$id"]},
                            ]
                        }
                    }
                }
            ],
            "as": "followed",
        }
    },
    {
        "$lookup": {
            "from": "edge.block",
            "let": {"id": "$_id"},
            "pipeline": [
                {
                    "$match": {
                        "$expr": {
                            "$or": [
                                {
                                    "$and": [
                                        {
                                            "$eq": [
                                                "$start",
                                                ObjectId("65486eae04cce43c5469e0f4"),
                                            ]
                                        },
                                        {"$eq": ["$end", "$$id"]},
                                    ]
                                },
                                {
                                    "$and": [
                                        {
                                            "$eq": [
                                                "$end",
                                                ObjectId("65486eae04cce43c5469e0f5"),
                                            ]
                                        },
                                        {"$eq": ["$start", "$$id"]},
                                    ]
                                },
                            ]
                        }
                    }
                }
            ],
            "as": "blocked",
        }
    },
    {"$match": {"followed": [], "blocked": []}},
    {"$group": {"_id": "$username", "phone": {"$first": {"$add": ["$phone", 10]}}}},
    {
        "$redact": {
            "$cond": {
                "if": {"$eq": ["phone", "132"]},
                "then": "$$KEEP",
                "else": "$$PRUNE",
            }
        }
    },
    {"$project": {"username": 0}},
    {"$skip": 5},
    {"$limit": 5},
    {"$out": "account"},
]

相关问题