I am trying to convert a working mongo query to bson in golang. I have the basic stuff down and working but am struggling to figure out how to integrate more advanced or
queries into the mix.
Anyone have a minute to help me convert the following query? It should hopefully give me the direction I need... Unfortunately I have not been able to find many examples outside of just evaluating and queries.
This works in mongo:
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 do I go about properly introducing the or
statement?
Introduction to Golang Type Conversion. In Go language, the type conversion is a way to change the data type of variable. We generally change the data from initially we defined to another type of data type for example if we have defined a variable as integer and same variable we are going to use for some other purposes.
In this guide, you can learn about how the Go Driver handles conversions between BSON and Go values. The process of converting a Go value to BSON is called marshalling, while the reverse process is called unmarshalling.
License A module to aid developers to generate Go BSON class maps. The auto-generated code output utilises go.mongodb.org/mongo-driver/bson, and is ideal to be used to read/write into MongoDB.
Package bson is a library for reading, writing, and manipulating BSON. BSON is a binary serialization format used to store documents and make remote procedure calls in MongoDB.
For completeness here is a full example of my last question in the comments above. The larger goal was dynamically building a bson query in go. Huge 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"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With