You can simply use the Database.CollectionNames() method, which returns the collection names present in this db. It returns a snippet in which you should check if your collection is included.
sess := ... // obtain session db := sess.DB("") // Get db, use db name if not given in connection url names, err := db.CollectionNames() if err := nil { // Handle error log.Printf("Failed to get coll names: %v", err) return } // Simply search in the names slice, eg for _, name := range names { if name == "collectionToCheck" { log.Printf("The collection exists!") break } }
But, as Neil Lunn wrote in his comments, you donβt need it. You must change your logic to use MongoDB so as not to rely on this check. Collections are created automatically if you try to insert a document, and queries from non-existent collections give no errors (and, of course, are not the result).
source share