[GODRIVER-2425] use nil slice in `$in` will cause "(BadValue) $in needs an array" Created: 17/May/22  Updated: 27/Oct/23  Resolved: 23/Jun/22

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

Type: Improvement Priority: Minor - P4
Reporter: yahui An Assignee: Benji Rewis (Inactive)
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-2137 empty/nil slices create null field, w... Backlog

 Description   

go version go1.18 windows/amd64

go.mongodb.org/mongo-driver v1.9.1

MongoDB server version: 5.0.2

package main
 
import (
	"context"
	"fmt"
 
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)
 
var mongoClient *mongo.Client
 
func connect() error {
	clientOptions := options.Client().
		ApplyURI("******").
		SetAuth(options.Credential{
			Username: "******",
			Password: "******",
		})
 
	client, err := mongo.Connect(context.TODO(), clientOptions)
	if err != nil {
		return fmt.Errorf("connect mongodb err: %w", err)
	}
 
	err = client.Ping(context.TODO(), nil)
	if err != nil {
		return fmt.Errorf("ping mongodb err: %w", err)
	}
 
	mongoClient = client
 
	return nil
}
 
func main() {
	if err := connect(); err != nil {
		panic(err)
	}
 
	// empty slice is ok
	test([]string{})
 
	// nil slice will panic
	var list []string
	test(list)
}
 
func test(list []string) {
	filter := bson.M{"any": bson.M{"$in": list}}
	_, err := mongoClient.Database("any").Collection("any").Find(context.TODO(), filter, nil)
	if err != nil {
		panic(err)
	}
}

the result is ``panic: (BadValue) $in needs an array``

now i have to check it before find , it's a little troublesome

func test(list []string) {
	if list == nil {
		list = []string{}
	}
 
	filter := bson.M{"any": bson.M{"$in": list}}
	_, err := mongoClient.Database("any").Collection("any").Find(context.TODO(), filter, nil)
	if err != nil {
		panic(err)
	}
}

can someone help me? thank you very much



 Comments   
Comment by PM Bot [ 23/Jun/22 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.

Comment by Benji Rewis (Inactive) [ 08/Jun/22 ]

Thanks for your report, anyahui741600@outlook.com! Apologies for the delay, the Go driver team has been preparing for MongoDB World, which is happening this week.

As you've noted in your example, the server is reporting that $in needs to be an array. This is because of how we Marshal slices in the Go driver. The "empty" slice has a value (is non-nil), and therefore Marshals to an empty BSON array. The nil slice has a nil value, and therefore Marshal to BSON null. The server errors for the latter, as $in does not accept BSON null and expects a BSON array.

As I mentioned in GODRIVER-2137, I see an argument to have nil slices in the Go driver Marshal to empty BSON arrays instead of BSON null. However, changing the driver to do so would break users' existing code that relies on the fact that something like var list []string is Marshaled as BSON null. We could not make a change like that until the next major release of the driver. I've marked this ticket as related to GODRIVER-2137.

As far as a workaround for you, I believe you could make use of the mgocompat registry instead of the default BSON registry. Using the mgocompat registry makes the BSON library follow behavior similar to the mgo driver (an old, community-built driver) that does encode nil slices as BSON null. Let me know if that works.

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