[GODRIVER-2847] CLONE - RunCommandCursor always fails and returns an error message referencing internal driver details Created: 13/May/23  Updated: 06/Dec/23  Resolved: 15/Jul/23

Status: Closed
Project: Go Driver
Component/s: Command & Dispatch
Affects Version/s: 1.11.6
Fix Version/s: 1.13.0, 1.13.1

Type: Improvement Priority: Major - P3
Reporter: Jacob Bills Assignee: Matt Dale
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File run_command_cursor.go    
Issue Links:
Cloners
clones GODRIVER-1114 RunCommandCursor always fails and ret... Closed
Duplicate
is duplicated by GODRIVER-1134 Database.RunCommandCursor is not work Closed
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Example code:

	_, err := client.Database("mydb").RunCommandCursor(nil, bson.D{
		{"explain", bson.D{{"find", "mycollection"}}},
		{"verbosity", "executionStats"},
	})

This simpler example fails in the same way:

	_, err := client.Database("mydb").RunCommandCursor(nil, bson.D{
		{"ping", 1},
	})

Error:

cursor should be an embedded document but is of BSON type invalid

It's quite possible that i'm just doing something wrong. In which, case the bug is just that the error message does not indicate what I did wrong. However, im pretty sure there's a real bug here preventing this from working.

I see that `RunCommandCursor` calls `ResultCursor`, which calls `NewCursorResponse` with `c.result`. `c.result` only appears to ever be set by `Execute` which, is never called. `NewCursorResponse` then returns the above error when it tries to verify the type of the "cursor" field on a nil document.



 Comments   
Comment by Githook User [ 15/Jul/23 ]

Author:

{'name': 'Matt Dale', 'email': '9760375+matthewdale@users.noreply.github.com', 'username': 'matthewdale'}

Message: GODRIVER-2847 Improve error message if RunCommandCursor command doesn't return a cursor. (#1317)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/995cb8d81a8dee33059a25ca5fc3306ce525a309

Comment by Matt Dale [ 14/Jul/23 ]

PR: https://github.com/mongodb/mongo-go-driver/pull/1317

Comment by Jacob Bills [ 18/May/23 ]

That makes sense and this works. I can confirm this error is returned for both aggregate() and find() commands when using explain. It is only slightly inconvenient since both aggregate and find I expect it to return a cursor in the routine I'm calling runcommand from. Thanks

Comment by Matt Dale [ 17/May/23 ]

Hey jacobtbills@gmail.com, I believe the issue is that the RunCommandCursor method expects the response to contain cursor information, but the explain and ping commands do not return a cursor (they return a document with the requested information). You should use the RunCommand method instead.

For example:

raw, err := client.Database("test").RunCommand(context.Background(), bson.D{
	{"explain", bson.D{{"find", "test"}}},
	{"verbosity", "executionStats"},
}).DecodeBytes()
if err != nil {
	panic(err)
}
fmt.Println(raw)

Does the above example work for your use case?

P.S. You're correct that the error message could be improved to suggest using the RunCommand method instead.

Comment by Jacob Bills [ 13/May/23 ]

Confirmed bug is still present in go driver v1.11.6

Comment by PM Bot [ 13/May/23 ]

Hi jacobtbills@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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