首先,我是个新手:)
我正在尝试使用go和mgo驱动程序在mongo中进行聚合+追加。
我的代码看起来像这样:
pipe := c.Pipe([]bson.M{{"$match": bson.M{"name":"John"}}})
iter := pipe.Iter()
resp := []bson.M{}
for iter.Next(&resp) {
//
// read "value.sha1" from each response
// do a:
// otherCollection.Upsert(bson.M{"value.sha1": mySha1}, resp)
//
}
来自聚合集合的响应可以有很多格式,所以我不能为它定义一个结构
我只需要从响应中获取一个字段,它是sha1,并根据sha1条件用收到的响应更新另一个集合。
有人能给我指正确的方向吗?
也许我误解了你,但你可以简单地将返回的文档作为map
访问。类似这样的东西:
pipe := c.Pipe([]bson.M{})
iter := pipe.Iter()
resp := bson.M{} // not array as you are using iterator which returns single document
for iter.Next(&resp) {
otherCollection.Upsert(bson.M{"value.sha1": result["value"].(bson.M)["sha1"]}, resp)
}