Skip to content

DOCSP-51818 Standardize replace usage ex #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: comp-cov
Choose a base branch
from

Conversation

lindseymoore
Copy link
Collaborator

@lindseymoore lindseymoore commented Jul 18, 2025

Pull Request Info

PR Reviewing Guidelines

JIRA - https://jira.mongodb.org/browse/DOCSP-51818

Staging Links

https://deploy-preview-541--docs-golang.netlify.app/crud/update/replace/#replace-one-document-example--full-file

Self-Review Checklist

  • Is this free of any warnings or errors in the RST?
  • Did you run a spell-check?
  • Did you run a grammar-check?
  • Are all the links working?
  • Are the facets and meta keywords accurate?
  • Are the page titles greater than 20 characters long and SEO relevant?

Copy link

netlify bot commented Jul 18, 2025

Deploy Preview for docs-golang ready!

Name Link
🔨 Latest commit 6e218b6
🔍 Latest deploy log https://app.netlify.com/projects/docs-golang/deploys/6883fa3999be1400089d8c03
😎 Deploy Preview https://deploy-preview-541--docs-golang.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@docs-builder-bot
Copy link

docs-builder-bot commented Jul 18, 2025

🔄 Deploy Preview for docs-golang processing

Item Details
🔨 Latest Commit d0e5687dd54b81e6937289695bee02984f9ec009
😎 Deploy Preview https://deploy-preview-541--docs-golang.netlify.app
🔍 Build Logs View Logs

Copy link
Collaborator

@stephmarie17 stephmarie17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! A tiny nit and a question 😄

fmt.Println("Number of documents replaced:", result.ModifiedCount)
}

// When you run this file for the first time, it should print:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] What do you think about removing vs keeping these comments at the end of the files? I think I've been removing since the output is shown in the code block but maybe we could align and make sure they're all consistent. I'm fine either way!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this particular comment, I think it's good to keep in! If you run this code example twice, it outputs nothing the second time because the document that is being queried for no longer exists.
I also think it's nice to keep because readers will hopefully copy this into their own code editors, and they can have the expected result without referencing the docs again. I am in favor of keeping them in if this makes sense to you!

@lindseymoore lindseymoore requested review from a team and prestonvasquez and removed request for a team July 21, 2025 23:18
coll := client.Database("sample_restaurants").Collection("restaurants")
filter := bson.D{{"name", "Madame Vo"}}
filter := RestaurantNameFilter{Name: "Rizzo's Fine Pizza"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lindseymoore Could you clarify the goal of this update? Is it to illustrate how users could use either a bson.D{} or Go struct as the filter argument for driver operations?

Copy link
Collaborator Author

@lindseymoore lindseymoore Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, exactly! We received this ticket from the Go PM to add both bson.D and structs to the usage examples : https://jira.mongodb.org/browse/DOCSP-51595 @prestonvasquez

@@ -23,7 +21,9 @@ type Restaurant struct {
Grades []interface{} `bson:"grades,omitempty"`
}

// end-restaurant-struct
type RestaurantNameFilter struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a struct as a filter is quite awkward and error prone. For example, say you have the following filter struct:

type RNFilter struct {
	Name string 
	Rating int 
}

In Go, if you don't populate both fields then the filter will use the "zero" value for Rating:

f := RNFilter{Name: "Rizzo's Fine Pizza"} // Rating is implicitly 0

So if the actual rating is anything other than "0", nothing will be found since the underlying command is something of this shape:

{"name": "Rizzo's Fine Pizza", "rating": 0}

Suggest retaining bson.D for the filter. Using a go struct for decoding results into is best practice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, let me check in with @rishitb-mongodb .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @prestonvasquez 's thought makes sense. Let's standardize on defining the queries via bson.D (i.e for any filters) and using the struct to represent the documents (when inserting new documents or retrieving results).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do.

@lindseymoore lindseymoore requested review from prestonvasquez and removed request for rishitb-mongodb July 25, 2025 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants