So sánh removeid với removeall trong golang mongodb năm 2024

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Show

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

In this guide, you can learn how to remove documents from your MongoDB collections using delete operations.

The example in this guide uses the following Book struct as a model for documents in the books collection:


type Book struct {

  Title  string

  Author string

  Length int32

}

To run the example in this guide, load the sample data into the`db.books` collection with the following snippet:


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

Each document contains a description of a book that includes the title, author, and page length corresponding to the title, author, and length fields in each document.

Tip

Nonexistent Databases and Collections

If the necessary database and collection don't exist when you perform a write operation, the server implicitly creates them.

Use delete operations to remove data from MongoDB. Delete operations consist of the following methods:

  • DeleteOne(), which deletes the first document that matches the filter
  • coll := client.Database("db").Collection("books") docs := []interface{}{ Book{Title: "Atonement", Author: "Ian McEwan", Length: 351}, Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331}, Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103}, Book{Title: "Outline", Author: "Rachel Cusk", Length: 258}, } result, err := coll.InsertMany(context.TODO(), docs)

    0, which deletes all documents that match the filter

Tip

If one document matches your filter when running the


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

0 method, it's equivalent to running the DeleteOne() method.

The DeleteOne() and


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

0 methods expect you to pass a


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

5 type and a


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

6 query filter specifying which documents to match.

They both optionally take a


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

7 type as a third parameter, which represents options you can use to configure the delete operation. If you don't specify a


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

7, the driver uses the default values for each option.

The


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

7 type allows you to configure options with the following methods:

Method

Description


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

0

The index to use to scan for documents to delete.

Default:


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

1


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

2

The type of language collation to use when sorting results.

Default:


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

1

The DeleteOne() and


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

0 methods return a


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

6 type. This type contains the


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

7 property, which states the number of documents deleted. If there are no matches to your filter, no document gets deleted and


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

7 is


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

9.

The following example performs the following with the


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

0 method:

  • Matches and deletes documents where the length is greater than `Book`2
  • Instructs the method to use the `Book`3 as the index


filter := bson.D{{"length", bson.D{{"$gt", 300}}}}

opts := options.Delete().SetHint(bson.D{{"_id", 1}})

result, err := coll.DeleteMany(context.TODO(), filter, opts)

if err != nil {

   panic(err)

}

fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

Tip

If the preceding example used the DeleteOne() method instead of


coll := client.Database("db").Collection("books")

docs := []interface{}{

  Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},

  Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},

  Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},

  Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},

}

result, err := coll.InsertMany(context.TODO(), docs)

0, the driver would delete the first of the two matched documents.

For runnable examples of the delete operations, see the following usage examples:

To learn more about performing the operations mentioned, see the following guides:

To learn about how the driver uses Context, see

To learn more about specifying hints, see

To learn more about collations, see

To learn more about any of the methods or types discussed in this guide, see the following API Documentation: