[GODRIVER-1800] cursor.All Example is incorrect Created: 20/Nov/20  Updated: 27/Oct/23  Resolved: 20/Nov/20

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Abhishek N/A Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#example-Cursor.All

Error Screenshot: https://pasteboard.co/JBdE2bd.png



 Comments   
Comment by Divjot Arora (Inactive) [ 25/Nov/20 ]

abhishek.ism.5035@gmail.com It's not possible to create an empty cursor directly. The driver mandates that cursors be created by running an operation like Find or Aggregate. Your idea to define a Cursor interface that is implemented by *mongo.Cursor and a new mock cursor type to implement it sounds reasonable to me.

– Divjot

Comment by Abhishek N/A [ 21/Nov/20 ]

Thanks for response Divjot, I needed to mock cursor for a unit test. One way is to add an interface which Cursor struct follows and mock the interface, is there any other way to create an empty cursor supported by mongodb golang package?

Comment by Divjot Arora (Inactive) [ 20/Nov/20 ]

Hi abhishek.ism.5035@gmail.com,

Thanks for the report. Because these examples are from standalone functions, they assume that the cursor was previously created using a different function. You can do this via a function like Find() or Aggregate():

cursor, err := collection.Find(ctx, bson.D{})
if err != nil { // handle }
defer cursor.Close(ctx)
 
// Use cursor to call All()

We're aware that it would be useful to have documentation and examples for using multiple functions at once, but it's tough to do this using the standard GoDoc/pkg.go.dev format, so we're working on ways to address this in the future. For now, I'm going to close this ticket, but feel free to leave a comment or open a new ticket if you have further questions.

– Divjot

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