大家。当我在 mongodb $in 查询选择器中使用 []uint8 数组时,会出现“(BadValue)$in 需要一个数组”错误。有人可以给我一些帮助吗?谢谢 !!!
这是我的复制步骤:
MongoDB信息
mongodb驱动版本为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 }
去代码
package main
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/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
}
白衣染霜花
临摹微笑
相关分类