Mongodb聚合使用文档字段作为数组结果的键而不是增量数字

发布于 2025-01-14 17:35:58 字数 1404 浏览 1 评论 0原文

我的聚合在 mongodb 查询中给了我这个结果

{"_id" : 1, "color" : "red", "year" : 2019},
{"_id" : 2, "color" : "blue", "year" : 2018},
{"_id" : 3, "color" : "green", "year" : 2020}

,在 php 中我得到了这样的结果:

array(3) {
    [0] => array(3) {
        ["_id"] => int(1),
        ["color"] => string(3) "red"
        ["year"] => int(2019),
    },
    [1] => array(3) {
        ["_id"] => int(2),
        ["color"] => string(4) "blue"
        ["year"] => int(2018),
    },
    [2] => array(3) {
        ["_id"] => int(3),
        ["color"] => string(5) "green"
        ["year"] => int(2020),
    }
}

我需要的是这样的结果:

array(3) {
    ["red"] => array(3) {
        ["_id"] => int(1),
        ["color"] => string(3) "red"
        ["year"] => int(2019),
    },
    ["blue"] => array(3) {
        ["_id"] => int(2),
        ["color"] => string(4) "blue"
        ["year"] => int(2018),
    },
    ["green"] => array(3) {
        ["_id"] => int(3),
        ["color"] => string(5) "green"
        ["year"] => int(2020),
    }
}

所以换句话说,我想将文档结果作为数组返回,键为 $color每个文档的。 我所做的是循环一次抛出结果并复制到一个新数组中,如下所示:

newArray = [];

foreach($query as $doc){
    $newArray[$doc['color']] = $doc;
}

现在 newArray 具有我需要的结构,但我想直接从 mongo 实现它,这样 php 服务器端就不必在存在时执行该工作有数千份文件。

My aggregation give me this results in mongodb query

{"_id" : 1, "color" : "red", "year" : 2019},
{"_id" : 2, "color" : "blue", "year" : 2018},
{"_id" : 3, "color" : "green", "year" : 2020}

and in php I have the results like this:

array(3) {
    [0] => array(3) {
        ["_id"] => int(1),
        ["color"] => string(3) "red"
        ["year"] => int(2019),
    },
    [1] => array(3) {
        ["_id"] => int(2),
        ["color"] => string(4) "blue"
        ["year"] => int(2018),
    },
    [2] => array(3) {
        ["_id"] => int(3),
        ["color"] => string(5) "green"
        ["year"] => int(2020),
    }
}

what I need is to have it like this:

array(3) {
    ["red"] => array(3) {
        ["_id"] => int(1),
        ["color"] => string(3) "red"
        ["year"] => int(2019),
    },
    ["blue"] => array(3) {
        ["_id"] => int(2),
        ["color"] => string(4) "blue"
        ["year"] => int(2018),
    },
    ["green"] => array(3) {
        ["_id"] => int(3),
        ["color"] => string(5) "green"
        ["year"] => int(2020),
    }
}

So in words, I want to return the documents results as an array with the key as the $color of each document.
What I did is to loop one time throw the results and copy in a new array as follow:

newArray = [];

foreach($query as $doc){
    $newArray[$doc['color']] = $doc;
}

and now newArray have the structure I need but I want to achieve it directly from mongo so the php server side not have to do the work when there are thousand of documents.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2025-01-21 17:35:58

听起来将这两个步骤添加到聚合中可以完成这项工作:

{$group: {_id: 0, arr: {$push: {k: '$color', v: {_id: '$_id', color: '$color', 
    year: '$year'}}}}},
{$project: {res: {$arrayToObject: '$arr'}, _id: 0}}

返回:

{
"res" : {
    "red" : {
        "_id" : 1,
        "color" : "red",
        "year" : 2019
    },
    "blue" : {
        "_id" : 2,
        "color" : "blue",
        "year" : 2018
    },
    "green" : {
        "_id" : 3,
        "color" : "green",
        "year" : 2020
    }
}
}

It sounds like adding these two steps to your aggregation can do the job:

{$group: {_id: 0, arr: {$push: {k: '$color', v: {_id: '$_id', color: '$color', 
    year: '$year'}}}}},
{$project: {res: {$arrayToObject: '$arr'}, _id: 0}}

Returning:

{
"res" : {
    "red" : {
        "_id" : 1,
        "color" : "red",
        "year" : 2019
    },
    "blue" : {
        "_id" : 2,
        "color" : "blue",
        "year" : 2018
    },
    "green" : {
        "_id" : 3,
        "color" : "green",
        "year" : 2020
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文