Home > other >  When using the mongodb $in query, the "(BadValue) $in needs an array" error occurs if [] u
When using the mongodb $in query, the "(BadValue) $in needs an array" error occurs if [] u

Time:12-14

everyone.When I use []uint8 array in mongodb $in query selector , "(BadValue) $in needs an array" error occurs. Could someone give me some help? Thanks !!!

Here are my reproduction steps:

Mongodb Info

mongodb driver version is v1.8.1

$ 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 }


Go Code

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
    }
}

Result

When I start the command on the command line, I get the following output:

go run main.go

(BadValue) $in needs an array

CodePudding user response:

uint8 is an alias to byte, and []byte is a special type, it's handled differently than other slice types (not as a slice of numbers). []byte values are encoded using bsoncodec.ByteSliceCodec and other slice values are encoded using bsoncodec.SliceCodec.

Use a slice of any other number type, e.g. []int8 or []int:

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

Note: the mongo driver has its own BSON implementation and package, do use that: go.mongodb.org/mongo-driver/bson. In your example you are importing and using gopkg.in/mgo.v2/bson which is a completely different BSON implementation, developed as part of the mgo driver (now unsupported and obsolete). Do not mix different drivers.

CodePudding user response:

You should remove conversion of i to uint8, and for getting data correct code like this.

// mock some data
collection := client.Database("testing").Collection("numbers")
for i := 0; i < 10; i   {
    _, err = collection.InsertOne(ctx, bson.M{"type": i, "value": i})
    if err != nil {
        fmt.Println(err)
        return
    }
}

 res := collection.FindOne(ctx, bson.M{
    "type": bson.M{
        "$in": []int{1, 2, 3},
    },
})
if res.Err()!=nil{
        // handle error
}

Then you can get data as raw or decode into another type, like:

 res.DecodeBytes()
  • Related