how to create bson map based on input

huangapple go评论76阅读模式
英文:

how to create bson map based on input

问题

我正在使用mgo库在golang中构建一个bson映射。我想重构一些代码以避免重复。

考虑以下代码:

bson.M {
"$match" : bson.M{
"xyz" : "abc",
},
"$id_1" : value_1,
}
和另一个类型的bson映射:

bson.M {
"$match" : bson.M{
"xyz" : "abc",
},
"$id_2" : value_2,
}

我如何将这两个结合起来(从一个函数中构建bson.M并返回,value1/2是该函数的参数),以便根据value_1是否为空字符串或value_2是否为空字符串来使用。

例如:如果我构建以下函数。我的意图是通过调用buildBsonObject("123","")来构建第一种类型的映射(参见上面的代码),通过调用buildBsonObject("", "456")来构建第二种类型的映射。

func buildBsonObject (value_1 string, value_2 string) {
return bson.M {
"$match" : bson.M{
"xyz" : "abc",
},
"$id_1" : value_1,
"$id_2" : value_2,
}
}

如果我这样做(参见buildBsonObject函数),那么当value_1为空字符串时,我的mongo聚合查询将无法工作,因为它会将id_1视为"",而我打算在我的bson对象中只有id_2。

有关如何解决这个问题的建议。谢谢。

英文:

I am building a bson map in golang using mgo library. I want to refactor some code to be able to avoid duplication.

Consider this:

    bson.M {
         "$match" : bson.M{
            "xyz" : "abc",
          },
          "$id_1" : value_1, 
    }

and another bson map of type:

    bson.M {
         "$match" : bson.M{
            "xyz" : "abc",
          },
          "$id_2" : value_2,
    }

How can I combine the two (build bson.M from a function and return, value1/2 are params to this func) to be used based on if value_1 is empty string or value_2 is empty string.

For example: if I build a the following function. My intention is to build map of first type (see above) by calling buildBsonObject("123","") and the second one by calling buildBsonObject("", "456").

func buildBsonObject (value_1 string, value_2 string) {    
    return bson.M {
             "$match" : bson.M{
                "xyz" : "abc",
              },
              "$id_1" : value_1, 
              "$id_2" : value_2,
        }
}

If I do the following (see function buildBsonObject), then when value_1 is empty string, my mongo aggregation query will not work since it will treat as id_1 being "", whereas I intended to just have id_2 in my bson object.

Any suggestions on how to do this. Thanks.

答案1

得分: 0

这是翻译好的内容:

如何看待这段代码:

func buildBsonObject(value_1 string, value_2 string) bson.M {
  m := bson.M{
    "$match": bson.M{
      "xyz": "abc",
    },
  }
  if value_1 != "" {
    m["$id_1"] = value_1
  }
  if value_2 != "" {
    m["$id_2"] = value_2
  }
  return m
}

[playground 示例](http://play.golang.org/p/Z09GrQr5gl)

另一种方法是将键名传递给函数

```go
func buildBsonObject(k string, v string) bson.M {
  return bson.M{
    "$match": bson.M{
      "xyz": "abc",
    },
    k: v,
  }
}

[playground 示例](http://play.golang.org/p/G3XRK1fKfY)
英文:

How about this:

func buildBsonObject(value_1 string, value_2 string) bson.M {
  m := bson.M{
	"$match": bson.M{
		"xyz": "abc",
	},
  }
  if value_1 != "" {
	m["$id_1"] = value_1
  }
  if value_2 != "" {
	m["$id_2"] = value_2
  }
  return m
}

playground example

An alternative is to pass the key name to the function:

func buildBsonObject(k string, v string) bson.M {
  return bson.M{
	"$match": bson.M{
		"xyz": "abc",
	},
	k: v,
  }
}

playground example

huangapple
  • 本文由 发表于 2015年12月2日 08:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/34032871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定