Exploring MongoDB queries
In this section, we will interact with the MongoDB server using CRUD operations, but first, let's create a database where the API data will be stored.
Note
You can view the full documentation for the MongoDB Go driver on the GoDoc website (https://godoc.org/go.mongodb.org/mongo-driver).
The InsertMany operation
Let's initialize the database with the recipes.json
file we created in the previous chapter. First, retrieve a Database
and then a Collection
instance from Client
. The Collection
instance will be used to insert documents:
func init() { Â Â Â recipes = make([]Recipe, 0) Â Â Â file, _ := ioutil.ReadFile("recipes.json") Â Â Â _ = json.Unmarshal([]byte(file), &recipes) Â Â Â ctx = context.Background() Â Â Â client, err = mongo.Connect(ctx, Â Â Â Â Â Â Â options.Client().ApplyURI(os.Getenv("MONGO_URI"))) Â Â &...