【问题标题】:Sorting on atomic element of denormalized array with mongodb aggregate request使用 mongodb 聚合请求对非规范化数组的原子元素进行排序
【发布时间】:2019-06-15 05:27:31
【问题描述】:

我的 Characters 集合中有以下数据:

[
    {
        "_id": "1",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 110 },
            { "name": "dexterity", "value": 50 },
            { "name": "health", "value": 200 }
        ]
    },
    {
        "_id": "2",
        "type": "archer",
        "attributes": [
            { "name": "precision", "value": 70 },
            { "name": "dexterity", "value": 80 },
            { "name": "health", "value": 150 }
        ]
    },
    {
        "_id": "3",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 80 },
            { "name": "dexterity", "value": 80 },
            { "name": "health", "value": 220 }
        ]
    },
    {
        "_id": "4",
        "type": "wizard",
        "attributes": [
            { "name": "power", "value": 100 },
            { "name": "dexterity", "value": 100 },
            { "name": "health", "value": 120 }
        ]
    },
    {
        "_id": "5",
        "type": "monk",
        "attributes": [
            { "name": "strength", "value": 50 },
            { "name": "intelligence", "value": 120 },
            { "name": "health", "value": 180 }
        ]
    },
    {
        "_id": "6",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 90 },
            { "name": "dexterity", "value": 75 },
            { "name": "health", "value": 200 }
        ]
    }
]

注意:“属性”是我模型中对象(名称 + 值)的非规范化数组。

我编写了一个通用服务来允许用户过滤我的数据。该服务根据用户输入(选择、条件、排序等)动态构建我的“聚合”查询。

我希望返回所有按 id 降序排列的战士。生成的查询将是:

Characters.aggregate().match({ "type": "warrior" }).sort({ "_id": -1 });

预期结果:

[
    {
        "_id": "6",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 90 },
            { "name": "dexterity", "value": 75 },
            { "name": "health", "value": 200 }
        ]
    },
    {
        "_id": "3",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 80 },
            { "name": "dexterity", "value": 80 },
            { "name": "health", "value": 220 }
        ]
    },
    {
        "_id": "1",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 110 },
            { "name": "dexterity", "value": 50 },
            { "name": "health", "value": 200 }
        ]
    }
]

现在我想显示按实力升序排列的战士列表。我无法对非规范化数组“属性”的特定元素进行排序,这是我的问题。

预期结果:

[
    {
        "_id": "3",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 80 },
            { "name": "dexterity", "value": 80 },
            { "name": "health", "value": 220 }
        ]
    },
    {
        "_id": "6",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 90 },
            { "name": "dexterity", "value": 75 },
            { "name": "health", "value": 200 }
        ]
    },
    {
        "_id": "1",
        "type": "warrior",
        "attributes": [
            { "name": "strength", "value": 110 },
            { "name": "dexterity", "value": 50 },
            { "name": "health", "value": 200 }
        ]
    }
]

我应该如何继续得到这个结果?

【问题讨论】:

    标签: mongodb sorting aggregate denormalization


    【解决方案1】:

    试试这个

    [
        {
            '$match': {
                'type': 'warrior'
            }
        }, {
            '$addFields': {
                "strength": {
                    $filter: {
                        input: "$attributes",
                        as: "attr",
                        cond: {$eq: [ "$$attr.name", "strength"] } 
                    }
                }
            }
        }, {
            '$sort': { "strength": 1 }
        }, {
            '$project': {
                'strength': 0
            }
        }
    ]
    

    【讨论】:

    • 我不明白这个请求......你如何明确排序是在属性“强度”而不是另一个属性上完成的? “强度”并不总是数组的第一个属性。
    猜你喜欢
    • 2020-09-10
    • 2019-04-29
    • 1970-01-01
    • 2016-09-24
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    相关资源
    最近更新 更多