gpt4 book ai didi

mongodb - 在不解码的情况下在 upsert 中使用 mgo 聚合迭代器数据

转载 作者:IT王子 更新时间:2023-10-29 02:13:39 25 4
gpt4 key购买 nike

首先,我是新手:)

我正在尝试使用 go 和 mgo 驱动程序在 mongo 中进行聚合 + upsert。

我的代码看起来像这样:

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)
}

关于mongodb - 在不解码的情况下在 upsert 中使用 mgo 聚合迭代器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com