Golang BSON transform

I am trying to convert a mongo working request to bson in golang. I have the basic stuff up and running, but I'm trying to figure out how to integrate more complex queries or

into the mix.

Anyone have a minute to help me transform the following query? It should hopefully give me the direction I need ... Unfortunately I haven't been able to find many examples other than evaluation and queries.

This works in mango:

db.my_collection.find({"$or": [
      {"dependencies.provider_id": "abc"}, 
      {"actions.provider_id": "abc"}]})

      

This works in golang / bson:

bson.M{"dependencies.provider_id": "abc"}

      

How can I enter the instruction correctly or

?

+3


source to share


2 answers


In your case, it would be:



bson.M{"$or": []bson.M{
    {"dependencies.provider_id": "abc"},
    {"actions.provider_id": "abc"},
}}

      

+7


source


For completeness, here's a complete example of my last question in the comments above. The bigger goal was to dynamically build the bson query in go. Many thanks to ANisus:



query := bson.M{}
query["origin"] = "test"
query["$or"] = []bson.M{}
query["$or"] = append(query["$or"].([]bson.M), bson.M{"abc": "1"})
query["$or"] = append(query["$or"].([]bson.M), bson.M{"def": "2"})

      

+5


source







All Articles