Deleting MongoDB Documents with Golang

Deleting MongoDB Documents with Golang

Golang & MongoDB - How to Delete Documents: Learn how to delete MongoDB documents and drop collections using the `DeleteOne`, `DeleteMany` and `Drop` functions of the Go driver for MongoDB.

The Requirements

There are a few requirements that should be met prior to starting this tutorial if you want maximum success:

  • Go 1.13+
  • MongoDB Go Driver 1.1.2+
  • MongoDB Atlas with an M0 cluster or better

In addition to having these requirements met, each must be properly configured.

You can get started with a forever free M0 cluster on MongoDB Atlas. In addition, you can apply the promotional code NRABOY200 to have premium credit added to your account.

If you need help connecting your Go application to MongoDB Atlas, I encourage you to check out my tutorial Quick Start: How to Get Connected to Your MongoDB Cluster with Go as it won't be explored in this particular tutorial.

Revisiting the Data Model for the Tutorial Series

Before we jump right into the removal of documents, it probably makes sense to revisit the data model we're going to be using to avoid confusion. If you're been keeping up with the previous tutorials in the series, you'll remember we are working with a podcasts collection and an episodes collection.

The podcasts collection has documents that look like this:

{
    "_id": ObjectId("5d9e0173c1305d2a54eb431a"),
    "title": "The Polyglot Developer Podcast",
    "author": "Nic Raboy"
}

The episodes collection has documents that look like the following:

{
    "_id": ObjectId("5d9f4701e9948c0f65c9165d"),
    "podcast": ObjectId("5d9e0173c1305d2a54eb431a"),
    "title": "Episode #1",
    "description": "This is a description for the first episode.",
    "duration": 25
}

When we start deleting documents or dropping collections, we'll be referencing fields from the above schemas and collection names.

Deleting a Single Document from a MongoDB Collection

Let's say that we want to delete a single document from one of our collections. We can make use of the DeleteOne function and provide a filter for the document that should be deleted. Take the following for example:

database := client.Database("quickstart")
podcastsCollection := database.Collection("podcasts")
result, err := podcastsCollection.DeleteOne(ctx, bson.M{"title": "The Polyglot Developer Podcast"})
if err != nil {
    log.Fatal(err)
}
fmt.Printf("DeleteOne removed %v document(s)\n", result.DeletedCount)

The above code obtains a handle to the database and collection that we wish to use from the client connection. Using the DeleteOne function, we can provide an application context as well as a filter. The filter in this example is around a podcast with a particular title.

If there was an error, we catch it and terminate the application, otherwise we use the result to print how many documents were deleted. Because we are using the DeleteOne function, we can only ever have a DeletedCount of zero or one.

Establishing a connection to the cluster and defining an application context can be seen in a previous tutorial of this getting started series.

Deleting Many Documents from a MongoDB Collection

There will often be scenarios where you need to remove more than one document from a collection in a single operation. For these tasks we can make use of the DeleteMany function on a collection, which behaves similar to the InsertMany and UpdateMany operations that we have already seen.

To see this in action, take a look at the following code:

database := client.Database("quickstart")
episodesCollection := database.Collection("episodes")
result, err = episodesCollection.DeleteMany(ctx, bson.M{"duration": 25})
if err != nil {
    log.Fatal(err)
}
fmt.Printf("DeleteMany removed %v document(s)\n", result.DeletedCount)

In the above example, pretty much everything is the same. The exception is that we're using a different collection and we're using the DeleteMany function. In this example, any document that has a field duration that equals 25 will be deleted.

In both the DeleteOne and DeleteMany examples both range and equality filters can be used. More information on the available operators can be found in the documentation.

Dropping a MongoDB Collection and All Documents within the Collection

Removing a single document or many documents isn't the only option. Entire collections can be dropped which would remove all documents and meta data, such as indexes, in the collection without using a filter. An example of this can be seen below:

if err = podcastsCollection.Drop(ctx); err != nil {
    log.Fatal(err)
}

Dropping an entire collection will return an error if something has failed. If the collection doesn't exist, the driver will mask the server error and in this case return a nil error.

The Final Code

To see this tutorial come together, you can take a look at a working example of the code below:

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
    client, err := mongo.NewClient(options.Client().ApplyURI("<ATLAS_URI_HERE>"))
    if err != nil {
        log.Fatal(err)
    }
    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
    err = client.Connect(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Disconnect(ctx)

    database := client.Database("quickstart")
    podcastsCollection := database.Collection("podcasts")
    episodesCollection := database.Collection("episodes")

    result, err := podcastsCollection.DeleteOne(ctx, bson.M{"title": "The Polyglot Developer Podcast"})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("DeleteOne removed %v document(s)\n", result.DeletedCount)

    result, err = episodesCollection.DeleteMany(ctx, bson.M{"duration": 25})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("DeleteMany removed %v document(s)\n", result.DeletedCount)

    if err = podcastsCollection.Drop(ctx); err != nil {
        log.Fatal(err)
    }

    if err = episodesCollection.Drop(ctx); err != nil {
        log.Fatal(err)
    }
}

If you try to run the code, don't forget to replace the MongoDB cluster information with that of your MongoDB Atlas cluster.

Conclusion

You just saw how to delete documents as well as drop collections from MongoDB using the Go programming language. In previous tutorials of the series we explored create, retrieve, and update, all of which are CRUD operations, where the final part is around delete.

Originally published by Nicolas Raboy at https://www.mongodb.com

golang go mongodb databases web-development

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

MongoDB Web development Services, Company in Chandigarh- QServices Inc

QServices Inc endow you with the wide spectrum of MongoDb database development services to build innovative, high-tech & versatile mobile and web applications with cutting edge database for numerous business requirements.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How to Build Your First Web Server with Golang

Learn how to build your first web server with Golang. Golang has a built-in HTTP package that contains utilities to help you create a web server quickly.

Best Web and App Development Company in Ahmedabad

Contact DataPierce for any questions or queries you have about website development, designing, marketing projects or any small or enterprise software development.

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.