1

I'm trying to connect to my mongo Atlas database using golang mongodb official driver. The problem is that the connection seems successful but the client won't retrieve anything. I tried to log ListDatabaseNames() and ListCollectionNames() but it always returns an empty array, like the db was empty. The connection string is correct. I am using the same one with a NodeJS project and it works just fine. Just copy-pasting the same db uri to a go project doesn't works. Here is the code:

// Use the SetServerAPIOptions() method to set the Stable API version to 1
    serverAPI := options.ServerAPI(options.ServerAPIVersion1)
    opts := options.Client().ApplyURI("mongodb+srv://<username>:<password>@my_mongodb_cluster.mongodb.net/?retryWrites=true&w=majority").SetServerAPIOptions(serverAPI)
    // Create a new client and connect to the server
    client, err := mongo.Connect(context.TODO(), opts)
    if err != nil {
        panic(err)
    }
    defer func() {
        if err = client.Disconnect(context.TODO()); err != nil {
            panic(err)
        }
    }()
    // Send a ping to confirm a successful connection
    if err := client.Database("MY_DB").RunCommand(context.TODO(), bson.D{{Key: "ping", Value: 1}}).Err(); err != nil {
        panic(err)
    }

    log.Println("Pinged your deployment. You successfully connected to MongoDB!")

    count, err := client.ListDatabaseNames(context.Background(), nil) // always returns []
    log.Println(count)
    if err != nil {
        log.Fatalln("Error", err)
    }

The snippet above is copied straight from mongodb Atlas. Here are some things to have in mind:

  1. It does not panic when providing a non existing database name client.Database("NON_EXISTING_DB_NAME")
  2. Copy-pasting the same uri to a nodejs project works perfectly.
  3. Changing the username and password fails the connection so I asume the credentials are correct.
  4. This exact code was working yesterday.

Any help or where should I start debugging will be appreciated. Thanks!

  • 1
    The document says: The filter parameter must be a document containing query operators and can be used to select which databases are included in the result. It cannot be nil. An empty document (e.g. bson.D{}) should be used to include all databases. See https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Client.ListDatabaseNames – Zeke Lu May 21 '23 at 10:53
  • Oh man.. Thats correct! In fact thats why it was working yesterday. Thanks a lot! – Michal Ružička Ružička May 21 '23 at 11:03
  • @ZekeLu what will be the reason for "It does not panic when providing a non existing database name" this as well? – PRATHEESH PC May 21 '23 at 11:08
  • The "Send a ping to confirm a successful connection" code should panic when providing a non existing database, but I doesn't. – Michal Ružička Ružička May 21 '23 at 11:17
  • If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform operations. Why do you think it should panic? – Zeke Lu May 21 '23 at 11:39
  • According to [this test](https://github.com/mongodb/mongo-go-driver/blob/f9ce69809fd84b5a6448a34d34b15975985333a1/mongo/client_test.go#L84-L85), the `err` in `count, err := client.ListDatabaseNames(context.Background(), nil)` should be non-nil. – Zeke Lu May 21 '23 at 11:43

1 Answers1

1

As stated by @Zeke Lu, the filter cannot be nil. A bson.D{} should be used then providing an empty filter.