0

I have a match table in my DB and I need to delete multiple items from it and I was wondering if there is a way to do this using a single query. I get deleteList of type []primitive.ObjectID in my Go code. deleteList is basically a list of match.id that need to be deleted. I could easily do it ranging through my slice of deleteList and deleting all the matches 1 by 1, but I feel like there should be a more efficient way of doing it in one query instead of flooding my db with multiple queries.

Does anyone know a possible solution for this?

Thank you.

1
  • 1
    Can you please share some code that you tried? Commented Sep 27, 2022 at 9:14

1 Answer 1

2

You may use Collection.DeleteMany().

Construct a filter that matches any of your IDs, and pass that to DeleteMany() like this:

c := ...                      // acquire match collection
ids := []primitive.ObjectID{} // the id list to delete

filter := bson.M{"_id": bson.M{"$in": ids}}

if _, err := c.DeleteMany(ctx, filter); err != nil {
    // handle error
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.