Skip to content

DOCSP-13863 delete many usage example #11

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions source/includes/usage-examples/code-snippets/deleteMany.go
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() {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))

if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()

// begin deleteMany
coll := client.Database("sample_mflix").Collection("movies")
filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}}

results, err := coll.DeleteMany(context.TODO(), filter)
// end deleteMany

if err != nil {
panic(err)
}

// When you run this file for the first time, it should print "Documents deleted: 4"
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
}
4 changes: 3 additions & 1 deletion source/includes/usage-examples/code-snippets/deleteOne.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func main() {

// begin deleteOne
coll := client.Database("sample_mflix").Collection("movies")
result, err := coll.DeleteOne(context.TODO(), bson.D{{"title", "Twilight"}})
filter := bson.D{{"title", "Twilight"}}

result, err := coll.DeleteOne(context.TODO(), filter)
// end deleteOne

if err != nil {
Expand Down
49 changes: 49 additions & 0 deletions source/usage-examples/deleteMany.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,52 @@ Delete Multiple Documents
=========================

.. default-domain:: mongodb

You can delete multiple documents in a collection by using the
``DeleteMany()`` method.

The following example specifies a query filter to the ``DeleteMany()``
method, which matches documents in the ``movies`` collection where the
``runtime`` field is greater than *800* and deletes all the matching
documents.

.. include:: /includes/usage-examples/run-example-tip.rst

.. literalinclude:: /includes/usage-examples/code-snippets/deleteMany.go
:start-after: begin deleteMany
:end-before: end deleteMany
:emphasize-lines: 4
:language: go
:dedent:

Click here <TODO> to see a fully runnable example.

Expected Result
---------------

After running the preceding code snippet, you should not be able to find
the following documents in the ``movies`` collection:

.. code-block:: json
:copyable: false

// results truncated
{ "_id": ObjectId("573a1397f29313caabce69db"), ... , "runtime": 1256, ... },
{ "_id": ObjectId("573a1397f29313caabce75fe"), ... , "runtime": 910, ... },
{ "_id": ObjectId("573a1399f29313caabcee1aa"), ... , "runtime": 1140, ... },
{ "_id": ObjectId("573a13a6f29313caabd18ae0"), ... , "runtime": 877, ... }

For an example on how to find multiple documents, see our :doc:`Find
Usage Example </usage-examples/find>`.

Additional Information
----------------------

For more information on deleting documents, specifying query filters,
and handling potential errors, see our guide on <TODO:
Deleting a Document>.

API Documentation
~~~~~~~~~~~~~~~~~

`DeleteMany() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.DeleteMany>`__
17 changes: 8 additions & 9 deletions source/usage-examples/deleteOne.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ Delete a Document
You can delete a 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.
The following example specifies a query filter to the ``DeleteOne()``
method, which matches documents in the ``movies`` collection where the
``title`` field is "Twilight", and deletes the first document that
matches.

.. include:: /includes/usage-examples/run-example-tip.rst

.. literalinclude:: /includes/usage-examples/code-snippets/deleteOne.go
:start-after: begin deleteOne
:end-before: end deleteOne
:emphasize-lines: 2
:emphasize-lines: 4
:language: go
:dedent:

Expand All @@ -29,12 +30,10 @@ After running the preceding code snippet, you should not be able to find
the following document in the ``movies`` collection:

.. code-block:: json
:copyable: false

{ "_id": { "$oid": "573a13bff29313caabd5e06b" },
...
"title": "Twilight",
...
}
// result truncated
{ "_id": ObjectId("573a13bff29313caabd5e06b"), ..., "title": "Twilight", ... }

For an example on how to find a document, see our :doc:`Find
One Usage Example </usage-examples/findOne>`.
Expand Down
4 changes: 2 additions & 2 deletions source/usage-examples/find.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ see the :manual:`MongoDB query operator reference documentation
API Documentation
~~~~~~~~~~~~~~~~~

- `Find() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.Find>`_
- `Cursor <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Cursor>`_
- `Find() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.Find>`__
- `Cursor <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Cursor>`__