@@ -219,41 +219,51 @@ func allReservedNames(client *mongo.Client) ([]string, error) {
219219 for _ , dbName := range dbs {
220220 fmt .Printf ("Database: %s\n " , dbName )
221221 db := client .Database (dbName )
222- collCursor , err := db . ListCollections (ctx , struct {}{} )
222+ names , err := getReservedNamesForDB (ctx , db , dbName )
223223 if err != nil {
224- return reservedNames , errors . Wrapf ( err , "ListCollections failed for db %s" , dbName )
224+ return reservedNames , err
225225 }
226- defer collCursor .Close (ctx ) //nolint:errcheck
227- for collCursor .Next (ctx ) {
228- var collInfo struct {
229- Name string `bson:"name"`
230- Type string `bson:"type"`
231- }
232- if err := collCursor .Decode (& collInfo ); err != nil {
233- return reservedNames , errors .Wrapf (err , "Decode failed for collection in db %s" , dbName )
234- }
235- reservedNames = append (reservedNames , collInfo .Name )
236- if collInfo .Type == "view" {
237- continue // skip views
238- }
239- coll := db .Collection (collInfo .Name )
240- cursor , err := coll .Indexes ().List (ctx )
241- if err != nil {
242- continue // skip if cannot list indexes
226+ reservedNames = append (reservedNames , names ... )
227+ }
228+
229+ return reservedNames , nil
230+ }
231+
232+ func getReservedNamesForDB (ctx context.Context , db * mongo.Database , dbName string ) ([]string , error ) {
233+ reservedNames := []string {}
234+ collCursor , err := db .ListCollections (ctx , struct {}{})
235+ if err != nil {
236+ return reservedNames , errors .Wrapf (err , "ListCollections failed for db %s" , dbName )
237+ }
238+ defer collCursor .Close (ctx ) //nolint:errcheck
239+ for collCursor .Next (ctx ) {
240+ var collInfo struct {
241+ Name string `bson:"name"`
242+ Type string `bson:"type"`
243+ }
244+ if err := collCursor .Decode (& collInfo ); err != nil {
245+ return reservedNames , errors .Wrapf (err , "Decode failed for collection in db %s" , dbName )
246+ }
247+ reservedNames = append (reservedNames , collInfo .Name )
248+ if collInfo .Type == "view" {
249+ continue // skip views
250+ }
251+ coll := db .Collection (collInfo .Name )
252+ cursor , err := coll .Indexes ().List (ctx )
253+ if err != nil {
254+ continue // skip if cannot list indexes
255+ }
256+ defer cursor .Close (ctx ) //nolint:errcheck
257+ for cursor .Next (ctx ) {
258+ var indexDoc map [string ]interface {}
259+ if err := cursor .Decode (& indexDoc ); err != nil {
260+ continue
243261 }
244- defer cursor .Close (ctx ) //nolint:errcheck
245- for cursor .Next (ctx ) {
246- var indexDoc map [string ]interface {}
247- if err := cursor .Decode (& indexDoc ); err != nil {
248- continue
249- }
250- if name , ok := indexDoc ["name" ].(string ); ok {
251- reservedNames = append (reservedNames , name )
252- }
262+ if name , ok := indexDoc ["name" ].(string ); ok {
263+ reservedNames = append (reservedNames , name )
253264 }
254265 }
255266 }
256-
257267 return reservedNames , nil
258268}
259269
0 commit comments