-
Notifications
You must be signed in to change notification settings - Fork 34
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
kyuan-mongodb
merged 8 commits into
mongodb:master
from
kyuan-mongodb:DOCSP-13863-DeleteManyUsageExample
Jul 30, 2021
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
73d756e
added DeleteMany usage example page
kyuan-mongodb 157a9bd
made code snippets more uniform with other pages
kyuan-mongodb f3d9fc5
updated api link
kyuan-mongodb 5851756
updated code snippet
kyuan-mongodb f3628d8
reformmated documents
kyuan-mongodb 41afa8f
reworded example statements
kyuan-mongodb 8a5ec5f
removed blank line
kyuan-mongodb a3f68cd
updated query filter
kyuan-mongodb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
source/includes/usage-examples/code-snippets/deleteMany.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}} | ||
kyuan-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
results, err := coll.DeleteMany(context.TODO(), filter) | ||
// end deleteMany | ||
|
||
if err != nil { | ||
log.Panic(err) | ||
kyuan-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// When you run this file for the first time, it should print "Documents deleted: 4" | ||
fmt.Printf("Documents deleted: %d\n", results.DeletedCount) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,49 @@ 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 that matches documents in | ||
the ``movies`` collection where the ``runtime`` field is greater than | ||
*800*, and deletes all the matching documents. | ||
kyuan-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. 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 | ||
|
||
{ "_id":{"$oid":"573a1397f29313caabce69db"}, ... "runtime":1256, ... }, | ||
{ "_id":{"$oid":"573a1397f29313caabce75fe"}, ... "runtime":910, ... }, | ||
{ "_id":{"$oid":"573a1399f29313caabcee1aa"}, ... "runtime":1140, ... }, | ||
{ "_id":{"$oid":"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>`__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>`__ | ||
kyuan-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.