如何从MongoDB集合中获取聚合 [英] How to Get an Aggregate from a MongoDB Collection
问题描述
以下集合包含不同交易所交易品种的交易品种(本例中 BTCUSD
和 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, :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 ),符号:BTCEUR,时间:ISODate(2013-04-16T00:00:00Z),打开:84.27,关闭:67.588,高:84.48,低:0.01,音量:26092.5432296}
如何获取 Go
中实际存在的符号列表?例如,我要找的结果是:
{BTCUSD,BTCEUR}
这可以让我从交换服务中检索最新的刻度值,而无需存储我感兴趣的符号。
从 mongo
shell我已经试过了...
db.candles.aggregate({$ group:{_id:$ symbol}})
...这里是结果:
{_id :BTCUSD}
{_id:BTCEUR}
什么是等价于 Go
使用 mgo
?是否有可能获得一组符号而不是__的集合:value
对?
您想要的是最简单的完成使用 collection.distinct()
。在MongoDB控制台中,它看起来像这样:
db.candles.distinct(symbol)
在Go中,使用 mgo
, Query.Distinct()
:
var sess * mgo.Session = ... //获取会话
c:= sess.DB(dbname)。 C(蜡烛)
var symbols [] string
err:= c.Find(nil).Distinct(symbol,& symbols)
if err! = nil {
log.Printf(Error:%v,err)
return
}
fmt.Println(symbols)
使用聚合框架
您也可以使用MongoDB聚合通过 Collection.Pipe()
方法。你必须传递一个切片,每个元素对应一个聚合阶段。
聚合的结果总是一个文档列表,所以如果你想要结果作为一段字符串,您必须手动执行转换。
var sess * mgo.Session = ... //获取会话
c:= sess.DB(dbname)。C(蜡烛)
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:符号}文件
//获得一段符号:
符号:= make([] string,len(results))
for i,doc:= range results {
$符号[i] = doc [_ id]。(字符串)
}
fmt.Println(符号)
有关更多技术如何获取一段值结果而不是文档,请参阅有没有一种方法可以查找切片()?
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?
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()?
这篇关于如何从MongoDB集合中获取聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!