How to write bson form of mongo query in golang?

302 views
Skip to first unread message

wilson20...@gmail.com

unread,
Jul 9, 2019, 8:49:06 AM7/9/19
to golang-nuts
 

I can query my mongodb collection to get the ipv4Addresses based on the nfType and the minimum distance using the command line query

db.nfinstancesdb.aggregate([
  {
    "$match": {
      "nfType": "AMF"
    }
  },
  {
    "$unwind": "$ipv4Addresses"
  },
  {
    $group: {
      "_id": "$distance",
      "ipv4Addresses": {
        "$addToSet": "$ipv4Addresses"
      }
    }
  },
  {
    "$sort": {
      "_id": 1
    }
  },
  {
    "$limit": 1
  }
])

This give the output am expecting as

[{"_id": 10,"ipv4Addresses": ["172.16.0.11","172.16.0.10"]}]

How can I write the bson form of the above query on Go?

I did in the function below but am getting all the ipv4Addresses instead of the above result.

func (m *NfInstanceDataAccess) FindIp(nfType string) ([]NfInstance, error) {
    var ip []NfInstance

    findQ := bson.M{"nfType": nfType}
    filter := bson.M{"ipv4Addresses": 1}
    err := db.C(COLLECTION).Find(findQ).Select(filter).All(&ip)
    if err != nil {
        return ip, err
    }
    return ip, nil
}

My collection has the following items

{
    "nfInstanceID": "1",
    "nfType": [
      "AMF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.10"
    ],
    "distance": 10
  },
  {
    "nfInstanceID": "2",
    "nfType": [
      "UPF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.20"
    ],
    "distance": 20
  },
  {
    "nfInstanceID": "3",
    "nfType": [
      "AMF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.30"
    ],
    "distance": 30
  },
  {
    "nfInstanceID": "4",
    "nfType": [
      "AMF"
    ],
    "nfStatus": [
      "REGISTERED"
    ],
    "ipv4Addresses": [
      "172.16.0.11"
    ],
    "distance": 10
  }

And I am expecting the same or similar output.

Burak Serdar

unread,
Jul 9, 2019, 9:36:36 AM7/9/19
to wilson20...@gmail.com, golang-nuts
On Tue, Jul 9, 2019 at 6:48 AM <wilson20...@gmail.com> wrote:
>
>
>
> I can query my mongodb collection to get the ipv4Addresses based on the nfType and the minimum distance using the command line query
>
> db.nfinstancesdb.aggregate([
> {
> "$match": {
> "nfType": "AMF"
> }
> },
> {
> "$unwind": "$ipv4Addresses"
> },
> {
> $group: {
> "_id": "$distance",
> "ipv4Addresses": {
> "$addToSet": "$ipv4Addresses"
> }
> }
> },
> {
> "$sort": {
> "_id": 1
> }
> },
> {
> "$limit": 1
> }
> ])
>
> This give the output am expecting as
>
> [{"_id": 10,"ipv4Addresses": ["172.16.0.11","172.16.0.10"]}]
>
> How can I write the bson form of the above query on Go?

That's not a query, that's an aggregation pipeline. The pipeline
collects all ip addresses, groups them by distance, and then outputs
them. So either you have to do what it does after getting all IP
addresses, or write the pipeline in bson:

pipeline:=[]bson.M{bson.M{"$match":{"nfType":"AMF"}},
bson.M{"$unwind":"$ipv4Addresses"},
bson.M{"..."}
...

db.C(COLLECTION).Pipe(pipeline).All(&ip)
> --
> 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/ead01e3b-2543-48ed-88e7-343e0748b28b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

daniel wilson

unread,
Jul 9, 2019, 11:02:30 AM7/9/19
to golang-nuts
Hi,

I have this pipeline in bson

pipe := db.C(COLLECTION).Pipe([]bson.M{
    {"$match":  bson.M{"nfType": "AMF"}},
    {"$unwind": bson.M{"$ipv4Addresses"}},
    {"$group":  bson.M{
                       "_id": bson.M{"$distance"},
                       "ipv4Addresses": bson.M{"$addToSet": "$ipv4Addresses"},
                      }}})
err := pipe.All(&ip)

but am getting error "missing key in map literal" in line

{"$unwind": bson.M{"$ipv4Addresses"}}, and "_id": bson.M{"$distance"}. I have tried changing
them to

{"$unwind": "$ipv4Addresses"}, and
bson.M{"_id": "$distance"}

but it not working.

Burak Serdar

unread,
Jul 9, 2019, 11:09:28 AM7/9/19
to daniel wilson, golang-nuts
On Tue, Jul 9, 2019 at 9:02 AM daniel wilson
<wilson20...@gmail.com> wrote:
>
> Hi,
>
> I have this pipeline in bson
>
> pipe := db.C(COLLECTION).Pipe([]bson.M{
> {"$match": bson.M{"nfType": "AMF"}},
> {"$unwind": bson.M{"$ipv4Addresses"}},
> {"$group": bson.M{
> "_id": bson.M{"$distance"},
> "ipv4Addresses": bson.M{"$addToSet": "$ipv4Addresses"},
> }}})
> err := pipe.All(&ip)
>
> but am getting error "missing key in map literal" in line
> {"$unwind": bson.M{"$ipv4Addresses"}}, and "_id": bson.M{"$distance"}. I have tried changing
> them to
>
> {"$unwind": "$ipv4Addresses"}, and
> bson.M{"_id": "$distance"}

These two are correct. What is not working?
> --
> 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/016ac7f1-775d-464a-b041-e72a5b3fbf39%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages