如何将两个聚合查询合并到on中

2024-09-28 03:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将这两个查询合并为一个:

查询1

db.Response.aggregate([ 
    {
        "$and": [
            { "job_details.owner_id" : 428 },
            { "job_details.owner_type" : 'searches' }
        ]
    },
    {
        "$group": {
            "_id": "$candidate_city_name_string",
            "count": { "$sum": 1 }
        }
    }
])

查询2

^{pr2}$

像这样的查询结果 输出1:

{
    "result": [
        { _id: 'Bangalore', count: 8 },
        { _id: 'cochi', count: 9 }
    ]
    "ok":1
}

输出2:

{
    "result": [
        { _id: 'java', count: 7 },
        { _id: 'python', count: 10 }
    ],
    "ok":1
}

如何在一个查询中得到这两个结果?在

我需要这样的输出:

预期输出:

 {
     "result": [
         "candidate_city_name_string": [
             { _id: 'Bangalore', count: 8 },
             { _id: 'cochi', count: 9 }
         ],
         "skill": [
             { _id: 'java', count: 7 },
             { _id: 'python', count: 10 }
         ]  
     ],
     "ok":1
 }

有可能吗?我在某处看到了关于$facet的东西,但我不明白。在


Tags: nameidcitydbstringcountjobok
1条回答
网友
1楼 · 发布于 2024-09-28 03:23:15
db.Response.aggregate([
{"$match":{"$and":[{"job_details.owner_id" : 482},{"job_details.owner_type" : 'searches'}]}}, 
{$facet: {
    "candidate_sublocation_name_string": [ 
                        {"$group": {"_id":"$candidate_sublocation_name_string","count": {"$sum": 1 }}}
                      ],
    "skill": [ 
                        {"$group": {"_id":"$skill","count": {"$sum": 1 }}}
                      ]
        }}])

相关问题 更多 >

    热门问题