-
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 3 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,40 @@ | ||
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", "Twilight"}}) | ||
// end deleteOne | ||
|
||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
// When you run this file for the first time, it should print "1 document was deleted." | ||
fmt.Printf("%d document was deleted.\n", result) | ||
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,51 @@ 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 "Twilight" in the ``title`` field, and | ||
deletes the first document that matches. | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. include:: /includes/quick-start/run-example-tip.rst | ||
|
||
.. literalinclude:: /includes/quick-start/deleteOne.go | ||
:start-after: begin deleteOne | ||
:end-before: end deleteOne | ||
:emphasize-lines: 2 | ||
: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 | ||
--------------- | ||
|
||
You should not be able to find the following document: | ||
|
||
.. code-block:: json | ||
|
||
{ "_id": { "$oid": "573a13bff29313caabd5e06b" }, | ||
... | ||
"title": "Twilight", | ||
... | ||
} | ||
|
||
To check if you successfully deleted this document, try performing a | ||
find operation with the ``title`` field on the collection. For an | ||
example of how to perform the find operation, see our <TODO: Find One | ||
Usage Example guide>. | ||
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.