Skip to content

Commit

Permalink
DOCSP-34174: codewhisperer pt 1 (#320)
Browse files Browse the repository at this point in the history
* DOCSP-34174: codewhisperer pt 1

* add comments

* NR PR fixes 1

* fix index

(cherry picked from commit 77557c6)
  • Loading branch information
rustagir committed Nov 28, 2023
1 parent cb00e61 commit 596153e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
6 changes: 3 additions & 3 deletions source/includes/fact-environments.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- `MongoDB Atlas
<https://www.mongodb.com/docs/atlas>`__: The fully
<https://www.mongodb.com/docs/atlas>`__: the fully
managed service for MongoDB deployments in the cloud
- :ref:`MongoDB Enterprise <install-mdb-enterprise>`: The
- :ref:`MongoDB Enterprise <install-mdb-enterprise>`: the
subscription-based, self-managed version of MongoDB
- :ref:`MongoDB Community <install-mdb-community-edition>`: The
- :ref:`MongoDB Community <install-mdb-community-edition>`: the
source-available, free-to-use, and self-managed version of MongoDB
13 changes: 13 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/limit.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Limits the number of documents returned by a query
package main

import (
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/projection.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Specifies which document fields to return
package main

import (
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/query.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Creates filters and retrieves documents that match the filters
package main

import (
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/retrieve.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Retrieves documents that match specified filters
package main

import (
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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{
Expand All @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
{+driver-long+}
=================

.. default-domain:: mongodb

.. facet::
:name: genre
:values: reference
Expand Down Expand Up @@ -34,11 +32,11 @@ You can add the driver to your application to work with MongoDB in Go.
Download it using `go get <https://pkg.go.dev/cmd/go/internal/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

Expand Down

0 comments on commit 596153e

Please sign in to comment.