go-bongo / bongo

Go ODM for MongoDB
MIT License
486 stars 40 forks source link

how to remove/delete a collection? #43

Open maxpaynestory opened 4 years ago

maxpaynestory commented 4 years ago

I am new to bongo. Could anybody please tell me how to remove/delete a collection?

yasaricli commented 4 years ago

There are two ways to delete.

If there is a document you want to remove Use DeleteDocument().

or you can use Delete() if you want to remove it with a query.

I can give an example for DeleteDocument as follows.

type User struct {
  bongo.DocumentBase `bson:",inline"`
  UserName           string `json:"username"`
}

// get user
user := &User{}
_ = connection.Collection("people").FindOne(bson.M{
  "username": "test",
}, user)

// DeleteDocument
err = connection.Collection("people").DeleteDocument(user)

log.Println(err)

If you want to remove it with a query, use Delete.

changeInfo, err := connection.Collection("people").Delete(bson.M{
  "username": "test",
})

log.Println(changeInfo, err)