MongoDB: Read

Learn how to read documents from MongoDB.

Now that we are capable of writing documents to MongoDB, let us take a look at how we can read them. As always, we must first implement our MongoDB package method for the same.

MongoDB’s read method

Press + to interact
func (mongodb *Mongo) Get(query *db.Query, target interface{}) error {
collection := mongodb.Client.
Database(query.Database).
Collection(string(query.Collection))
context, cancel := context.WithTimeout(context.Background(), TIMEOUT)
result := collection.FindOne(context, query.Filter)
defer cancel()
err := result.Decode(target)
return err
}

The Get function is primarily directed by the query ...