[GODRIVER-995] FindOne() doesn't find one with nil filter Created: 24/Apr/19  Updated: 27/Oct/23  Resolved: 25/Apr/19

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: 1.0.1, 1.0.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: David L. Parsley Assignee: Divjot Arora (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux, Go 1.12.4, Mongo Docker container created 2019-04-12T00:21:50.281225741Z



 Description   

I was writing code to just grab a TODO item with no filter, but kept getting no document. I was able to easily reproduce with the Trainer example; here's a snippet:

// create a value into which the result can be decoded
var result Trainer

//err = collection.FindOne(context.TODO(), filter).Decode(&result)
err = collection.FindOne(context.TODO(), nil).Decode(&result)
if err != nil

{ log.Fatal(err) }

With nil for filter:
2019/04/24 10:41:32 document is nil

If I connect using mongo shell:
> db.trainers.findOne()

{ "_id" : "ash", "name" : "Ash", "age" : 11, "city" : "Pallet Town" }

I'm working around by supplying a filter that's true for all records.



 Comments   
Comment by Divjot Arora (Inactive) [ 25/Apr/19 ]

parsley42 The driver's operations don't support using nil where a document is expected. Like you mentioned in the description, you should use a filter that matches everything such asĀ 

collection.FindOne(context.TODO(), bson.D{})

Comment by David L. Parsley [ 25/Apr/19 ]

To be perhaps more clear, I'm referring to reproducing this issue with your go driver tutorial: https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial

Generated at Thu Feb 08 08:35:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.