duanjing7651 2017-11-19 16:56 采纳率: 0%
浏览 268
已采纳

如何从MongoDB集合中获取聚合

The following collection contains the ticks for the diverse exchange symbols (in this case BTCUSD and BTCEUR):

{ "_id" : ObjectId("5a08d2b956df9b2302759d1a"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-13T00:00:00Z"), "open" : 112, "close" : 91.1, "high" : 130, "low" : 81.12, "volume" : 23866.6770456 }
{ "_id" : ObjectId("5a08d2b956df9b2302759d1c"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-14T00:00:00Z"), "open" : 91.1, "close" : 90.171, "high" : 109, "low" : 20, "volume" : 16437.2196645 }
{ "_id" : ObjectId("5a08d2b956df9b2302759d1e"), "symbol" : "BTCEUR", "time" : ISODate("2013-04-15T00:00:00Z"), "open" : 89.86, "close" : 83.302, "high" : 104, "low" : 71.497, "volume" : 16393.12856398 }
{ "_id" : ObjectId("5a08d2b956df9b2302759d20"), "symbol" : "BTCEUR", "time" : ISODate("2013-04-16T00:00:00Z"), "open" : 84.27, "close" : 67.588, "high" : 84.48, "low" : 0.01, "volume" : 26092.5432296 }

How do I get the list of symbols actually present in the collection in Go? For instance, the result I'm looking for is:

{ "BTCUSD", "BTCEUR" }

This would let me retrieve the latest tick values from the exchange service without having to store somewhere the symbols I'm interested in.

From the mongo shell I've tried this...

db.candles.aggregate({$group: {_id:"$symbol"}})

... and here is the result:

{ "_id" : "BTCUSD" }
{ "_id" : "BTCEUR" }

What is the equivalent in Go using mgo? Is it possible to just get an array of symbols instead of a collection of "_id" : "value" pairs?

  • 写回答

1条回答 默认 最新

  • douyingyu5573 2017-11-20 09:23
    关注

    Using Distinct()

    What you want is easiest done using collection.distinct(). In MongoDB console it would look like this:

    db.candles.distinct("symbol")
    

    And in Go, using mgo, Query.Distinct():

    var sess *mgo.Session = ... // Acquire session
    c := sess.DB("dbname").C("candles")
    
    var symbols []string
    err := c.Find(nil).Distinct("symbol", &symbols)
    if err != nil {
        log.Printf("Error: %v", err)
        return
    }
    fmt.Println(symbols)
    

    Using the Aggregation Framework

    You can also do it using MongoDB aggregation, which is available via the Collection.Pipe() method. You have to pass a slice to it, each element corresponds to an aggregation stage.

    The result of an aggregation is always a list of documents, so if you want the result as a slice of strings, you have to do the "conversion" manually.

    var sess *mgo.Session = ... // Acquire session
    c := sess.DB("dbname").C("candles")
    
    pipe := c.Pipe([]bson.M{
        {
            "$group": bson.M{
                "_id": "$symbol",
            },
        },
    })
    var results = []bson.M{}
    if err := pipe.All(&results); err != nil {
        log.Printf("Error: %v", err)
        return
    }
    
    // results holds {"_id": "symbol"} documents
    // To get a slice of symbols:
    symbols := make([]string, len(results))
    for i, doc := range results {
        symbols[i] = doc["_id"].(string)
    }
    fmt.Println(symbols)
    

    For more techniques how to get a slice of values result instead of documents, see Is there a way to get slice as result of Find()?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵