Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用接口{}动态查询mongodb_Mongodb_Go - Fatal编程技术网

如何使用接口{}动态查询mongodb

如何使用接口{}动态查询mongodb,mongodb,go,Mongodb,Go,我试图使用如下所示的Go接口进行动态mongodb查询 func (m *NfInstanceDataAccess) FindIp(preferredNfInstances string, args ...interface{}) ([]model.NfProfile, bool) { var ip []model.NfProfile pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": a

我试图使用如下所示的Go接口进行动态mongodb查询

func (m *NfInstanceDataAccess) FindIp(preferredNfInstances string, args ...interface{}) ([]model.NfProfile, bool) {
    var ip []model.NfProfile
    pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": args, "allowedNssais.sd": args}
    filter := bson.M{"ipv4Addresses": true}
    err := db.C(COLLECTION).Find(pipeline).Select(filter).All(&ip)
    if err != nil {
        return ip, false
    }
    return ip, true
}
将http服务器处理程序中的函数用作

    nfInstanceIp, success := Da.FindIp(targetNfType, sst, sd)
            if !success {
                WriteError(response, ErrInternalServer)
                return
            }
但是响应nfInstanceIp始终为空,即使我的mongodb集合中存在与FindIp参数匹配的值。 当我在下面的代码中使用integer和string等其他类型时,一切都正常

func (m *NfInstanceDataAccess) FindIp(preferredNfInstances string, sst int, sd string) ([]model.NfProfile, bool) {
    var ip []model.NfProfile
    pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": sst, "allowedNssais.sd": sd}
    filter := bson.M{"ipv4Addresses": true}
    err := db.C(COLLECTION).Find(pipeline).Select(filter).All(&ip)
    if err != nil {
        return ip, false
    }
    return ip, true
}
有人能给我解释一下为什么接口的使用不起作用,以及如何动态地编写这个函数吗

按照建议修改函数以使用mongodb$或类似以下代码的逻辑

func (m *NfInstanceDataAccess) FindIp(preferredNfInstances string, args ...interface{}) ([]model.NfProfile, bool) {
    var ip []model.NfProfile
    pipeline := bson.M{
        "nfType": preferredNfInstances,
        "$or": []interface{}{
            bson.M{"sNssais.sst": args[0].(int32), "sNssais.sd": args[1].(string)},
            bson.M{"amfInfo.taiList.tac": args},
            bson.M{"smfInfo.taiList.tac": args},
            bson.M{"upfInfo.taiList.tac": args},
        },
    }
    filter := bson.M{"ipv4Addresses": true}
    err := db.C(COLLECTION).Find(pipeline).Select(filter).All(&ip)
    if err != nil {
        return ip, false
    }
    return ip, true
}
逻辑$or不起作用。只有当我的FindIp输入匹配时,它才起作用

bson.M{"sNssais.sst": args[0].(int32), "sNssais.sd": args[1].(string)}
但即使将类型转换设置为args,其他输入也不起作用
您知道如何使用mongodb logical$或吗?

您需要执行索引,如果需要,还需要执行类型转换

pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": args[0].(int), "allowedNssais.sd": args[1].(string)}

pipeline := bson.M{"nfType": preferredNfInstances, "allowedNssais.sst": args[0], "allowedNssais.sd": args[1]}