【问题标题】:Can i aggregate two mongodb queries in one query using golang (mgo)?我可以使用 golang (mgo) 在一个查询中聚合两个 mongodb 查询吗?
【发布时间】:2017-02-06 09:48:00
【问题描述】:

我使用golangmgomongodb version is 3.2.9

例如,我在一个集合中有两个文档:

{"groupId" : 4, "name" : "email", "value" : "11@11.com"}

{"groupId" : 4,"name" : "phoneNumber","value" : "000000000"}

我知道phoneNumber (value and name),我需要查找电子邮件(价值)。 可以通过两个查询简单地完成:首先通过 phoneNumber 我找到了 groupId,然后通过 groupId 我找到了 email。 是否可以在一个查询中完成(使用 golang 和 mgo)?

【问题讨论】:

    标签: mongodb go aggregation-framework mgo mongodb-aggregation


    【解决方案1】:

    是的,您需要运行以下形式的聚合管道:

    var pipeline = [    
        {
            "$group": {
                "_id": "$groupId",
                "entries": {
                    "$push": {
                        "name": "$name",
                        "value": "$value"
                    }
                }
            }
        },
        {
            "$match": {
                "entries.name" : "phoneNumber", 
                "entries.value" : "000000000"
            }
        },
        {
            "$project": {
                "item": {
                    "$arrayElemAt": [
                        {
                            "$filter": {
                                "input": "$entries",
                                "as": "item",
                                "cond": { "$eq": [ "$$item.name", "email" ] }
                            }
                        }, 0
                    ]
                }
            }
        },
        {
            "$project": {
                "_id": 0,
                "email": "$item.value"
            }
        }
    
    ]);
    db.collection.aggregate(pipeline);
    

    样本输出

    { "email" : "11@11.com" }
    

    下面是等效的 mGo 表达式(未经测试):

    pipeline := []bson.D{   
        bson.M{
            "$group": bson.M{
                "_id": "$groupId",
                "entries": bson.M{
                    "$push": bson.M{
                        "name": "$name",
                        "value": "$value"
                    }
                }
            }
        },
        bson.M{
            "$match": bson.M{
                "entries.name" : "phoneNumber", 
                "entries.value" : "000000000"
            }
        },
        bson.M{
            "$project": bson.M{
                "item": bson.M{
                    "$arrayElemAt": [
                        bson.M{
                            "$filter": bson.M{
                                "input": "$entries",
                                "as": "item",
                                "cond": bson.M{ "$eq": 
                                    []interface{}{  "$$item.name", "email" }    
                                }
                            }
                        }, 0
                    ]
                }
            }
        },
        bson.M{
            "$project": bson.M{
                "_id": 0,
                "email": "$item.value"
            }
        }
    }
    
    pipe := collection.Pipe(pipeline)
    iter := pipe.Iter()
    

    【讨论】:

      猜你喜欢
      • 2020-09-18
      • 2023-01-31
      • 2022-10-04
      • 2016-11-24
      • 2023-03-28
      • 2020-07-15
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多