登录
首页 >  Golang >  Go问答

为什么在使用 mongodb $in 查询时,如果使用 uint8 进行查询,会出现“(BadValue) $in need an array”错误?

来源:stackoverflow

时间:2024-02-12 09:27:25 151浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《为什么在使用 mongodb $in 查询时,如果使用 uint8 进行查询,会出现“(BadValue) $in need an array”错误?》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

大家。当我在 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
        }
    }

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

正确答案


uint8byte 的别名,[]byte 是一种特殊类型,它的处理方式与其他切片类型不同(不是数字切片)。 []byte 值使用 bsoncodec.ByteSliceCodec 进行编码,其他切片值使用 bsoncodec.SliceCodec 进行编码。

使用任何其他数字类型的切片,例如[]int8[]int:

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

注意:mongo 驱动程序有自己的 bson 实现和包,请使用:go.mongodb.org/mongo-driver/bson。在您的示例中,您导入并使用 gopkg.in/mgo.v2/bson,这是一个完全不同的 bson 实现,作为一部分开发mgo 驱动程序(现已不受支持且已过时)。请勿混合使用不同的驱动程序。

您应该删除 iuint8 的转换,以及像这样获取数据正确的代码。

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

然后您可以获取原始数据或解码为另一种类型,例如:

res.DecodeBytes()

终于介绍完啦!小伙伴们,这篇关于《为什么在使用 mongodb $in 查询时,如果使用 uint8 进行查询,会出现“(BadValue) $in need an array”错误?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>