-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-13862 delete one usage example #6
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
Changes from 2 commits
ecceaf7
2fed0bb
ed28820
8327c4a
a46f60e
d692984
f951e74
63a705e
64ee139
c56dcc2
f240370
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
// Replace the uri string with your MongoDB deployment's connection string. | ||
const uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority" | ||
|
||
func main() { | ||
|
||
ctx := context.TODO() | ||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { | ||
if err = client.Disconnect(ctx); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
// begin deleteOne | ||
coll := client.Database("sample_mflix").Collection("movies") | ||
|
||
result, err := coll.DeleteOne(ctx, bson.D{{"title", "The Room"}}) | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// end deleteOne | ||
|
||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
fmt.Printf("%v document was deleted.\n", result.DeletedCount) | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// After running this file, it should print "1 document was deleted." | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.. tip:: | ||
|
||
Read the :doc:`Usage Examples guide </usage-examples>` to learn how | ||
to run this example. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,38 @@ Delete a Document | |
================= | ||
|
||
.. default-domain:: mongodb | ||
|
||
You can delete a single document in a collection using the | ||
``DeleteOne()`` method. | ||
|
||
The following example specifies a query filter that matches documents in | ||
the ``movies`` collection with the value "The Room" in the ``title`` field, and | ||
deletes the first document that matches. | ||
|
||
.. include:: /includes/quick-start/run-example-tip.rst | ||
|
||
.. literalinclude:: /includes/quick-start/deleteOne.go | ||
:start-after: begin deleteOne | ||
:end-before: end deleteOne | ||
:emphasize-lines: 3 | ||
:language: go | ||
:dedent: | ||
|
||
Click here <TODO> to see a fully runnable example. | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Expected Result | ||
--------------- | ||
|
||
After you run the code example, you cannot find the deleted document. | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Additional Information | ||
---------------------- | ||
|
||
For more information on deleting documents, specifying query filters, | ||
and handling potential errors, see our guide on <TODO: | ||
fundamental delete a doc>. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
:go-api:`DeleteOne() <mongo#Collection.DeleteOne>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment as on #5: Please update to link to the docs at https://pkg.go.dev/go.mongodb.org/mongo-driver instead. |
Uh oh!
There was an error while loading. Please reload this page.