Golang mongodb remove all items from the collection

How to remove all items from a collection stored in mongodb using GO lang?

In the mongo console, I can use:

db.mycollection.remove({})

where empty brackets {} mean the entire document template.

There are methods in GO lang (I use "gopkg.in/mgo.v2" and "gopkg.in/mgo.v2/bson"):

sess.DB("mydb").C("mycollection").Remove(...)
or
sess.DB("mydb").C("mycollection").RemoveAll(...)

but for both of them you need a parameter that implements the selector. For example, the selector may be a bson card

bson.M{"id": id}

but I want to delete all the elements, not the individual ones.

+4
source share
2 answers

See MongoDB documentation at: http://docs.mongodb.org/manual/tutorial/remove-documents/

, RemoveAll . , :

sess.DB("mydb").C("mycollection").RemoveAll(nil)

.

+5

@DidierSpezia C("mycollection").RemoveAll. , JSON " " {} "null", , , map[string]interface{} bson.M.

sess.DB("mydb").C("mycollection").RemoveAll(bson.M{})
0

Source: https://habr.com/ru/post/1569699/


All Articles