将 MongoDB 函数 foreach 转换为 mgo (Golang) 函数
converting MongoDB function foreach into mgo (Golang) function
此函数尝试根据其值更新匹配的代码
res collection
因为有 code of Marque
它将与 doc.Marque
进行比较,如果是这种情况,它将被 value of the marque
替换。
此代码在 mongoDB CLI
中运行良好,但因为我正在使用 GO
。
我尝试将其转换为 mgo
,如下所示,但它不起作用,我在 mgo
中没有找到 foreach 函数,是否有可以替换的内容案件 ?感谢您的帮助
db.res.find().forEach(function(doc){
var v = db.brands.findOne({code: doc.Marque});
if(v){
db.res.update({"Marque": doc.Marque},
{$set: {"Marque":v.value}}, {multi: true});
}
});
这是我尝试过的:
result:=Results{}
pipe:=res.find(bson.M{}).Iter()
for pipe.Next(&result) {
brands:=brands.findOne({code: doc.Marque});
if(v){
pipe.update({"Marque": doc.Marque},
{$set: {"Marque": v.value}}, {multi: true});
}
}
访问 mgo Godoc 可能会帮助您了解其工作原理。
其次,在Golang中导出types/functions都是以大写字母开头。所以 res.find
, brands.findOne
, ... 应该分别是 res.Find
, brands.FineOne
,如果有这样的函数的话。
// let's say you have a type like this
type myResult struct {
ID bson.ObjectId `bson:"_id"`
Marque string `bson:"Marque"`
// other fields...
}
// and another type like this
type myCode struct {
Code string `bson:"code"`
// other fields...
}
res := db.C("res")
brands := db.C("brands")
result := myResult{}
// iterate all documents
iter := res.Find(nil).Iter()
for iter.Next(&result) {
var v myCode
err := brands.Find(bson.M{"code": result.Marque}).One(&v)
if err != nil {
// maybe not found or other reason,
// it is recommend to have additional check
continue
}
query := bson.M{"_id": result.ID}
update := bson.M{"Marque": v.value}
if err = res.Update(query, update); err != nil {
// handle error
}
}
if err := iter.Close(); err != nil {
fmt.Println(err)
}
此函数尝试根据其值更新匹配的代码
res collection
因为有 code of Marque
它将与 doc.Marque
进行比较,如果是这种情况,它将被 value of the marque
替换。
此代码在 mongoDB CLI
中运行良好,但因为我正在使用 GO
。
我尝试将其转换为 mgo
,如下所示,但它不起作用,我在 mgo
中没有找到 foreach 函数,是否有可以替换的内容案件 ?感谢您的帮助
db.res.find().forEach(function(doc){
var v = db.brands.findOne({code: doc.Marque});
if(v){
db.res.update({"Marque": doc.Marque},
{$set: {"Marque":v.value}}, {multi: true});
}
});
这是我尝试过的:
result:=Results{}
pipe:=res.find(bson.M{}).Iter()
for pipe.Next(&result) {
brands:=brands.findOne({code: doc.Marque});
if(v){
pipe.update({"Marque": doc.Marque},
{$set: {"Marque": v.value}}, {multi: true});
}
}
访问 mgo Godoc 可能会帮助您了解其工作原理。
其次,在Golang中导出types/functions都是以大写字母开头。所以 res.find
, brands.findOne
, ... 应该分别是 res.Find
, brands.FineOne
,如果有这样的函数的话。
// let's say you have a type like this
type myResult struct {
ID bson.ObjectId `bson:"_id"`
Marque string `bson:"Marque"`
// other fields...
}
// and another type like this
type myCode struct {
Code string `bson:"code"`
// other fields...
}
res := db.C("res")
brands := db.C("brands")
result := myResult{}
// iterate all documents
iter := res.Find(nil).Iter()
for iter.Next(&result) {
var v myCode
err := brands.Find(bson.M{"code": result.Marque}).One(&v)
if err != nil {
// maybe not found or other reason,
// it is recommend to have additional check
continue
}
query := bson.M{"_id": result.ID}
update := bson.M{"Marque": v.value}
if err = res.Update(query, update); err != nil {
// handle error
}
}
if err := iter.Close(); err != nil {
fmt.Println(err)
}