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"))) &...