如何使用mongo-go-driver 0.2.0在bson.A中使用切片的元素

How do I use a given slice to generate a bson.A? I need this to make dynamic queries.

I am upgrading from mongo-go-driver alpha to beta 0.2.0, and this has become a major change in my project since the API is different now.

https://godoc.org/github.com/mongodb/mongo-go-driver/bson

input := []string{"bar", "world"}
//expected output
bson.A{"bar", "world"}

So bson.A underlying type is []interface{},

I could just use append to it like a slice

Not sure if you ever found your answer, but I battled with this for an hour+ yesterday.

I'm not positive this will solve what you're asking. I assume you're trying to build a filter from your input. Ultimately I did not use a bson.A when I was trying to pass an array.

Situation: Trying to build a filter and one of the bson.D elements is an array.

I thought I needed to use bson.A.

My initial assumption:

return bson.D{
  {"uuid", request.Uuid},
  {"action", request.Action},
  {"resource", bson.D{{"$in", bson.A{resourceStrings}}}},
}

where resourceStrings is a slice of strings.

However this will ultimately build a filter that looks like FILTER : [ {resource [{$in [[Orgs::Organizations::1 Orgs::Organizations::* Orgs::Organizations Orgs::*]]}]}]

*Note that the $in method is looking for an array of arrays here.

What we want is: FILTER : [{uuid 80} {action UpdateOrganization} {resource [{$in [Orgs::Organizations::1 Orgs::Organizations::* Orgs::Organizations Orgs::*]}]}]

If we pass in a literal array of strings it will work...

return bson.D{
  {"uuid", request.Uuid},
  {"action", request.Action},
  {"resource", bson.D{{"$in", bson.A{"Orgs::Organizations::1", "Orgs::Organizations::*", "Orgs::Organizations", "Orgs::*"}}}},
}

After some trial and error I found that bson.D will directly accept the array.

ultimately I solved the issue thus

return bson.D{
  {"uuid", request.Uuid},
  {"action", request.Action},
  {"resource", bson.D{{"$in", resourceStrings}}},
}

Taking your example literally - if you're simply trying to marshal an array to a bson.A try:

bson.A{input}