diff --git a/source/includes/fact-environments.rst b/source/includes/fact-environments.rst index 0cae4338..a7b3bdbd 100644 --- a/source/includes/fact-environments.rst +++ b/source/includes/fact-environments.rst @@ -1,7 +1,7 @@ - `MongoDB Atlas - `__: The fully + `__: the fully managed service for MongoDB deployments in the cloud -- :ref:`MongoDB Enterprise `: The +- :ref:`MongoDB Enterprise `: the subscription-based, self-managed version of MongoDB -- :ref:`MongoDB Community `: The +- :ref:`MongoDB Community `: the source-available, free-to-use, and self-managed version of MongoDB diff --git a/source/includes/fundamentals/code-snippets/CRUD/limit.go b/source/includes/fundamentals/code-snippets/CRUD/limit.go index 99e2c1bc..21c33f5a 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/limit.go +++ b/source/includes/fundamentals/code-snippets/CRUD/limit.go @@ -1,3 +1,4 @@ +// Limits the number of documents returned by a query package main import ( @@ -56,10 +57,15 @@ func main() { fmt.Println("\nLimit:\n") { + // Creates a filter to match documents that have an + // "enrollment" value greater than 20 //begin limit filter := bson.D{{"enrollment", bson.D{{"$gt", 20}}}} + + // Sets a limit to return the first 2 matched documents opts := options.Find().SetLimit(2) + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter, opts) var results []Course @@ -75,10 +81,15 @@ func main() { fmt.Println("\nLimit, Skip, and Sort:\n") { + // Creates an empty filter to match all documents //begin multi options filter := bson.D{} + + // Sets options to sort by descending order on "enrollment", + // return only 2 documents, and skip the first matched document opts := options.Find().SetSort(bson.D{{"enrollment", -1}}).SetLimit(2).SetSkip(1) + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter, opts) var results []Course @@ -94,9 +105,11 @@ func main() { fmt.Println("\nAggregation Limit:\n") { + // Creates a limit stage to return 3 documents // begin aggregate limit limitStage := bson.D{{"$limit", 3}} + // Aggregates results and prints them as structs cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage}) if err != nil { panic(err) diff --git a/source/includes/fundamentals/code-snippets/CRUD/projection.go b/source/includes/fundamentals/code-snippets/CRUD/projection.go index 9975b07f..43066a00 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/projection.go +++ b/source/includes/fundamentals/code-snippets/CRUD/projection.go @@ -1,3 +1,4 @@ +// Specifies which document fields to return package main import ( @@ -54,10 +55,15 @@ func main() { fmt.Println("\nExclude Projection:\n") { + // Creates an empty filter to match all documents //begin exclude projection filter := bson.D{} + + // Sets a projection to exclude the "course_id" and "enrollment" fields opts := options.Find().SetProjection(bson.D{{"course_id", 0}, {"enrollment", 0}}) + // Retrieves all documents and prints them as structs without + // the specified fields cursor, err := coll.Find(context.TODO(), filter, opts) if err != nil { panic(err) @@ -76,10 +82,15 @@ func main() { fmt.Println("\nInclude Projection:\n") { + // Creates an empty filter to match all documents //begin include projection filter := bson.D{} + + // Sets a projection to include the "title" and "enrollment" fields opts := options.Find().SetProjection(bson.D{{"title", 1}, {"enrollment", 1}}) + // Retrieves all documents and prints them as structs including + // only the specified fields cursor, err := coll.Find(context.TODO(), filter, opts) if err != nil { panic(err) @@ -98,9 +109,13 @@ func main() { fmt.Println("\nAggregation Projection:\n") { + // Creates a projection stage to include only the "title" and + // "course_id" fields // begin aggregate projection projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"course_id", 1}}}} + // Aggregates results and prints them as structs including + // only the specified fields cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{projectStage}) if err != nil { panic(err) diff --git a/source/includes/fundamentals/code-snippets/CRUD/query.go b/source/includes/fundamentals/code-snippets/CRUD/query.go index 3884d587..827bedf3 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/query.go +++ b/source/includes/fundamentals/code-snippets/CRUD/query.go @@ -1,3 +1,4 @@ +// Creates filters and retrieves documents that match the filters package main import ( @@ -62,8 +63,10 @@ func main() { fmt.Println("\nLiteral Value:\n") { + // Creates a filter to match documents that have a "type" value of "Oolong" filter := bson.D{{"type", "Oolong"}} + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter) if err != nil { panic(err) @@ -81,8 +84,10 @@ func main() { fmt.Println("\nComparison:\n") { + // Creates a filter to match documents that have a "rating" value below 7 filter := bson.D{{"rating", bson.D{{"$lt", 7}}}} + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter) if err != nil { panic(err) @@ -100,6 +105,7 @@ func main() { fmt.Println("\nLogical:\n") { + // Creates a filter to match documents that have a "rating" value less than or equal to 10 and greater than 7 filter := bson.D{ {"$and", bson.A{ @@ -109,6 +115,7 @@ func main() { }, } + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter) if err != nil { panic(err) @@ -126,8 +133,10 @@ func main() { fmt.Println("\nElement:\n") { + // Creates a filter to match documents that do not contain the "vendor" field filter := bson.D{{"vendor", bson.D{{"$exists", false}}}} + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter) if err != nil { panic(err) @@ -145,8 +154,10 @@ func main() { fmt.Println("\nEvaluation:\n") { + // Creates a filter to match documents that have a "type" value starting with the letter "E" filter := bson.D{{"type", bson.D{{"$regex", "^E"}}}} + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter) if err != nil { panic(err) @@ -164,8 +175,10 @@ func main() { fmt.Println("\nArray:\n") { + // Creates a filter to match documents where the "vendor" array contains "C" filter := bson.D{{"vendor", bson.D{{"$all", bson.A{"C"}}}}} + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter) if err != nil { panic(err) @@ -183,8 +196,10 @@ func main() { fmt.Println("\nBitwise:\n") { + // Creates a filter to match documents where the "rating" value has the same bits set as 6 filter := bson.D{{"rating", bson.D{{"$bitsAllSet", 6}}}} + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter) if err != nil { panic(err) diff --git a/source/includes/fundamentals/code-snippets/CRUD/retrieve.go b/source/includes/fundamentals/code-snippets/CRUD/retrieve.go index 97b8c330..2d24c70a 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/retrieve.go +++ b/source/includes/fundamentals/code-snippets/CRUD/retrieve.go @@ -1,3 +1,4 @@ +// Retrieves documents that match specified filters package main import ( @@ -61,6 +62,7 @@ func main() { fmt.Println("\nFind:\n") { + // Creates a filter to match documents that have a "rating" value between 5 and 9 // begin find docs filter := bson.D{ {"$and", @@ -72,6 +74,7 @@ func main() { sort := bson.D{{"date_ordered", 1}} opts := options.Find().SetSort(sort) + // Retrieves documents that match the filter and prints them as structs cursor, err := coll.Find(context.TODO(), filter, opts) if err != nil { panic(err) @@ -90,10 +93,14 @@ func main() { fmt.Println("\nFind One:\n") { + // Creates a filter to match documents that have a + // "date_ordered" value before December 2009 // begin find one docs filter := bson.D{{"date_ordered", bson.D{{"$lte", time.Date(2009, 11, 30, 0, 0, 0, 0, time.Local)}}}} opts := options.FindOne().SetSkip(2) + // Retrieves a document that matches the filter and prints it as + // a struct var result Review err := coll.FindOne(context.TODO(), filter, opts).Decode(&result) if err != nil { @@ -113,9 +120,13 @@ func main() { panic(err) } + // Creates a filter to match a document that has the specified + // "_id" value filter := bson.D{{"_id", id}} opts := options.FindOne().SetProjection(bson.D{{"item", 1}, {"rating", 1}}) + // Retrieves a document that matches the filter and prints it as + // a struct var result Review err = coll.FindOne(context.TODO(), filter, opts).Decode(&result) if err != nil { @@ -129,6 +140,8 @@ func main() { fmt.Println("\nAggregation:\n") { + // Creates an aggregation to group documents by "item" and finds + // the average "rating" value // begin aggregate docs groupStage := bson.D{ {"$group", bson.D{ @@ -143,6 +156,7 @@ func main() { panic(err) } + // Prints the average "rating" for each item var results []bson.M if err = cursor.All(context.TODO(), &results); err != nil { panic(err) diff --git a/source/index.txt b/source/index.txt index 533b94fc..1f19a087 100644 --- a/source/index.txt +++ b/source/index.txt @@ -2,8 +2,6 @@ {+driver-long+} ================= -.. default-domain:: mongodb - .. facet:: :name: genre :values: reference @@ -34,11 +32,11 @@ You can add the driver to your application to work with MongoDB in Go. Download it using `go get `__ or set up a runnable project by following our Quick Start guide. -Compatibility -------------- +Connect to a Compatible MongoDB Deployment +------------------------------------------ -You can use the {+driver-short+} to connect to deployments hosted in the -following environments: +You can use the {+driver-short+} to connect to MongoDB +deployments running on one of the following hosted services or editions: .. include:: /includes/fact-environments.rst