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?
result
? Also, try the find withctx.TODO()
. In general, it is a better idea to usebson.D
for defining the query filter (bson.M cannot guarantee the order of entries - relevant with multiple filter conditions). – prasad_result
is defined astype Result struct { Password string
json:"password"` Answer stringjson:"answer
}. And I'll try that
ctx.TODO()` – Patrick Collins