[GODRIVER-2254] when use $in []uint8 query in go , will get (BadValue) $in needs an array Created: 13/Dec/21  Updated: 27/Oct/23  Resolved: 19/Jan/22

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

Type: Bug Priority: Unknown
Reporter: sink smell Assignee: Benji Rewis (Inactive)
Resolution: Gone away Votes: 0
Labels: Bug, driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

When using the go mongodb driver,  $in query, the (BadValue) $in needs an array error occurs if [] uint8 is used for the query.

bug may be ? Our business has been affected.

mongo-driver v1.8.1

 mongodb instance is a shard cluster

 

$ mongo --host 192.168.64.6
MongoDB shell version v4.0.3
connecting to: mongodb://192.168.64.6:27017/
Implicit session: session { "id" : UUID("e4d7cea2-ab81-45ad-a51e-e7acf45a7242") }
MongoDB server version: 4.4.8
WARNING: shell and server versions do not match
 
mongos> use testing
switched to db testing
mongos> db.numbers.find()
{ "_id" : ObjectId("61b71d3d73b251bceee62032"), "type" : 0, "value" : 0 }
{ "_id" : ObjectId("61b71d3d73b251bceee62033"), "type" : 1, "value" : 1 }
{ "_id" : ObjectId("61b71d3d73b251bceee62034"), "type" : 2, "value" : 2 }
{ "_id" : ObjectId("61b71d3d73b251bceee62035"), "type" : 3, "value" : 3 }
{ "_id" : ObjectId("61b71d3d73b251bceee62036"), "value" : 4, "type" : 4 }
{ "_id" : ObjectId("61b71d3d73b251bceee62037"), "value" : 5, "type" : 5 }
{ "_id" : ObjectId("61b71d3d73b251bceee62038"), "type" : 6, "value" : 6 }
{ "_id" : ObjectId("61b71d3d73b251bceee62039"), "type" : 7, "value" : 7 }
{ "_id" : ObjectId("61b71d3d73b251bceee6203a"), "type" : 8, "value" : 8 }
{ "_id" : ObjectId("61b71d3d73b251bceee6203b"), "type" : 9, "value" : 9 }
mongos>​

 

 

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

How to Reproduce

package main
 
import (
	"context"
	"fmt"
	"time"
 
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"gopkg.in/mgo.v2/bson"
)
 
func main() {
	// init mongodb client
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://192.168.64.6:27017/"))
	if err != nil {
		fmt.Println(err)
		return
	}
 
	// mock some data
	collection := client.Database("testing").Collection("numbers")
	for i := 0; i < 10; i++ {
		_, err = collection.InsertOne(ctx, bson.M{"type": uint8(i), "value": i})
		if err != nil {
			fmt.Println(err)
			return
		}
	}
 
	// query
	filter := bson.M{"type": bson.M{"$in": []uint8{1, 2, 3}}}
	res := collection.FindOne(ctx, filter)
	if err = res.Err(); err != nil {
		fmt.Println(err)
		return
	}
}

go run main.go
 
(BadValue) $in needs an array​

Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by PM Bot [ 19/Jan/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) [ 04/Jan/22 ]

Apologies for the delay sinksmell@163.com; we were off for the holidays. I don't think we can actually distinguish between a []uint8 and a []byte in the Go driver since they are type-aliased. See this example in the Go playground for why type assertions/casts fail to work as expected here. Did the workaround I suggested (using either bson.A in your FindOne filter or an array of a different integer type) work? Let me know.

Comment by Benji Rewis (Inactive) [ 15/Dec/21 ]

Hello sinksmell@163.com! Thanks again for your report. This is indeed odd behavior, and I believe our BSON library is interpreting {{[]uint8

{1, 2, 3}}} as a byte slice (since the byte type is an alias for uint8 in Golang) instead of an actual array of numbers. You should be able to use

 


filter := bson.M{"type": bson.M{"$in": bson.A{1, 2, 3}

}}

 


As your filter and get the correct behavior, but I understand that might be inconvenient.

 

I'll continue to investigate.

Comment by Benji Rewis (Inactive) [ 14/Dec/21 ]

Thanks for your bug report, sinksmell@163.com! We're taking a look now.

Comment by sink smell [ 14/Dec/21 ]

use  "go.mongodb.org/mongo-driver/bson" also occurs  ```

(BadValue) $in needs an array​```

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