How to query mongodb dynamically using Go interface

119 views
Skip to first unread message

afriyie...@gmail.com

unread,
Jan 16, 2020, 10:04:40 AM1/16/20
to golang-nuts
Hi,

I have been trying to create a dynamic mongodb query using golang interface but the logical $or does not work.
It only return a documents when the input matches the bson.M{"sNssais.sst": args[0].(int32), "sNssais.sd": args[1].(string)}.
other matches like bson.M{"amfInfo.taiList.tac": args}, etc does not work even though a document in mongodb collection exist that matches the input value.
Any idea as how to do this? The function is as below

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
}



burak serdar

unread,
Jan 16, 2020, 10:27:08 AM1/16/20
to afriyie...@gmail.com, golang-nuts
On Thu, Jan 16, 2020 at 8:03 AM <afriyie...@gmail.com> wrote:
>
> Hi,
>
> I have been trying to create a dynamic mongodb query using golang interface but the logical $or does not work.
> It only return a documents when the input matches the bson.M{"sNssais.sst": args[0].(int32), "sNssais.sd": args[1].(string)}.
> other matches like bson.M{"amfInfo.taiList.tac": args}, etc does not work even though a document in mongodb collection exist that matches the input value.
> Any idea as how to do this? The function is as below

That depends on what's in args, and in amfInfo.taiList.tac, etc. args
is an array, so if the *.tac fields are also arrays, you're looking
for an exact match. Is that really the case?

>
> 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
> }
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c2dca0c6-e3f7-4822-9b7f-b09102450293%40googlegroups.com.

afriyie...@gmail.com

unread,
Jan 16, 2020, 11:08:59 AM1/16/20
to golang-nuts
Hi,

this is the JSON document in mongodb.
{
    "sNssais": [{
        "sst": 0,
        "sd": "string"
    }],
    "nsiList": [
        "string"
    ],
    "ipv4Addresses": [
        "198.51.100.1"
    ],
    "ipv6Addresses": [
        "2001:db8:85a3::8a2e:370:7334"
    ],
    "amfInfo": {
        "amfSetId": "string",
        "taiList": [{
            "plmnId": {
                "mcc": "string",
                "mnc": "string"
            },
            "tac": "3022",
            "nid": "string"
        }],
    },
    "customInfo": {}
}

The args in the *.tac are string values, even if i set the type casting like bson.M{"amfInfo.taiList.tac": args[0].(string)} it stiil do not match "3022" args input.
And yes, am looking for exact match.

burak serdar

unread,
Jan 16, 2020, 11:28:58 AM1/16/20
to afriyie...@gmail.com, golang-nuts
That query with args[0] should work, provided args[0] is "3022". I
suggest you check the args values.

>
>
>
> On Thursday, January 16, 2020 at 5:04:40 PM UTC+2, Afriyie Abraham Kwabena wrote:
>>
>> Hi,
>>
>> I have been trying to create a dynamic mongodb query using golang interface but the logical $or does not work.
>> It only return a documents when the input matches the bson.M{"sNssais.sst": args[0].(int32), "sNssais.sd": args[1].(string)}.
>> other matches like bson.M{"amfInfo.taiList.tac": args}, etc does not work even though a document in mongodb collection exist that matches the input value.
>> Any idea as how to do this? The function is as below
>>
>> 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
>> }
>>
>>
>>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/37f74556-974f-4229-bd2f-6850bfa62620%40googlegroups.com.

afriyie...@gmail.com

unread,
Jan 17, 2020, 3:24:46 AM1/17/20
to golang-nuts
Hi,
The problem wa about the index, i solved it by using the right index
bson.M{"amfInfo.taiList.tac": args[2]} since the fuction is called

Da.FindIp(targetNfType, sst, sd, tac)

Thanks!

On Thursday, January 16, 2020 at 5:04:40 PM UTC+2, Afriyie Abraham Kwabena wrote:
Reply all
Reply to author
Forward
0 new messages