-
Type: Bug
-
Resolution: Gone away
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: BSON
-
None
If an uninitialized bson.D is passed to Find, the following error results:
cannot transform type primitive.D to a BSON Document: WriteNull can only write while positioned on a Element or Value but is positioned on a TopLevel
This is a very confusing error for users. As mentioned in GODRIVER-917, we differ from mgo in how we serialize a nil document, but regardless of how we resolve that ticket, I think it would be a better user experience to detect this case earlier in the call chain so we can return a meaningful error message that would help users quickly fix the problem.
I suspect that any API method (or option) that takes a document could have the same problem as Find.
Here is a repro (against master):
package main import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { opts := options.Client().ApplyURI("mongodb://localhost:27017") client, err := mongo.Connect(nil, opts) if err != nil { panic(err) } collection := client.Database("test").Collection("test") var filter bson.D cursor, err := collection.Find(nil, filter) if err != nil { panic(err) } _ = cursor }
- related to
-
GODRIVER-854 Err: cannot transform type bson.D to a BSON Document
- Closed
-
GODRIVER-917 Difference in BSON/JSON serialization of nil primitive.D compared to mgo
- Closed