Patrick Collins
Patrick Collins

Reputation: 6131

mongodb go: `mongo: no documents in result`

I have a pretty simple entry in my mongoDB database:

{"_id":{"$oid":"609b15511a048e03dda05861"},"password":"test_password","answer":"test_answer"}

And when I use filter parameters in the Atlas UI, I am able to pull up results.

Filter:

{"password": "test_password"}

However, when I call to the DB, I keep getting the error mongo: no documents in result

filter := bson.M{"password": "test_password"}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err := collection.FindOne(ctx, filter).Decode(&result)
if err != nil {
    log.Printf("%v", err)
}

I appear to be connecting properly to the collection. Any thoughts?

Upvotes: 0

Views: 11614

Answers (1)

Patrick Collins
Patrick Collins

Reputation: 6131

I wasn't connecting properly to the db after all.

I was connecting to my collection like so:

collection := client.Database("DB_NAME").Collection("COLLECTION_NAME")

And since it didn't throw an error, I incorrectly assumed this was right. You can look at your collections and databases with the following:

databases, _ := client.ListDatabaseNames(ctx, bson.M{})
log.Printf("%v", databases)
collections, _ := client.Database("DATABASES").ListCollectionNames(ctx, bson.M{})
log.Printf("%v", collections)

Upvotes: 1

Related Questions