Home  >  Article  >  Backend Development  >  Go language (mgo) delete documents in MongoDB

Go language (mgo) delete documents in MongoDB

PHPz
PHPzOriginal
2023-04-14 11:16:51526browse

When using MGO to connect to MongoDB for document operations in Golang, deleting documents is a common operation that needs to be done. This article will introduce you how to use mgo to delete documents in MongoDB.

Delete documents

Delete a single document

First, let’s look at how to use mgo to delete a single document.

session, err := mgo.Dial("localhost:27017")
if err != nil {
    panic(err)
}
defer session.Close()

// 选择数据库和集合
collection := session.DB("test_db").C("test_collection")

err = collection.Remove(bson.M{"name": "John"})
if err != nil {
    panic(err)
}

In this example, we connected to MongoDB and selected the database "test_db" and the collection "test_collection". We then create a matching query using bson.M, find the document named "John", and delete it using the collection.Remove() method. If deletion fails, we will throw an exception.

Delete multiple documents

If you need to delete multiple documents, you can use the collection.RemoveAll() method.

session, err := mgo.Dial("localhost:27017")
if err != nil {
    panic(err)
}
defer session.Close()

// 选择数据库和集合
collection := session.DB("test_db").C("test_collection")

_, err = collection.RemoveAll(bson.M{"gender": "male"})
if err != nil {
    panic(err)
}

In this example, we use bson.M to create a query that finds all documents with the gender "male" and use the collection.RemoveAll() method to remove them all.

Delete Collection

If you want to completely delete a collection and free up its memory and hard disk space, you can use the collection.Drop() method.

session, err := mgo.Dial("localhost:27017")
if err != nil {
    panic(err)
}
defer session.Close()

// 选择数据库和集合
collection := session.DB("test_db").C("test_collection")

err = collection.Drop()
if err != nil {
    panic(err)
}

In this example, we delete the "test_collection" collection using the collection.Drop() method. Note that all documents will be permanently deleted.

Summary

When using mgo to connect to MongoDB for document operations in Golang, deleting documents is a frequently required operation. To delete a single document, use the collection.Remove() method, to delete multiple documents, use the collection.RemoveAll() method, and to completely delete a collection, use the collection.Drop() method.

Hope this article can help you use mgo in Golang to delete documents in MongoDB.

The above is the detailed content of Go language (mgo) delete documents in MongoDB. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:c How to implement golangNext article:c How to implement golang