Convert cli query to Go bson query

Hi

How can I convert the following mongo cli query to Go bson query:

db.getCollection('province').find({"provincename" : "somename"},{"localmunicipalities.municipality":1})

I have this in my Go function
opt := options.Find()
opt.SetProjection(bson.D{{“localmunicipalities.municipality”, 1}})

cur, err := collection.Find(context.TODO(), bson.D{{"provincename", provinceName}}, opt)

Thanks

Hi @Steven_Venter, welcome !

The example Go snippet code that you provided should return the same result as your mongo CLI query. Could you please elaborate further the problem that you’re experiencing ?

Could you also provide the following:

  • MongoDB Go driver version
  • An example document
  • Desired output
  • Error messages, if any

Regards,
Wan.

Hi Wan

I don’t get any error messages, just empty data and only 1 record.

Diver is go.mongodb.org/mongo-driver v1.3.3

This is a sample but I truncated it

{
    "_id" : ObjectId("5ec0fc0be1bf7aa04ac28ddb"),
    "provincename" : "Northern Cape",
    "localmunicipalities" : [ 
        {
            "municipality" : "!Kheis Local",
            "url" : "/overview/1181/kheis-local-municipality",
            "municipalcities" : [ 
                "Brandboom", 
                "Groblershoop"
            ]
        }, 
        {
            "municipality" : "Dawid Kruiper Local",
            "url" : "/overview/1245/dawid-kruiper-local-municipality",
            "municipalcities" : [ 
                "Mier", 
                "Rietfontein", 
                "Upington"
            ]
        }, 
        {
            "municipality" : "Dikgatlong Local",
            "url" : "/overview/1160/dikgatlong-local-municipality",
            "municipalcities" : [ 
                "Barkly West", 
                "Delportshoop", 
                "Windsorton"
            ]
        }, 
        {
            "municipality" : "Emthanjeni Local",
            "url" : "/overview/1173/emthanjeni-local-municipality",
            "municipalcities" : [ 
                "Britstown", 
                "De Aar", 
                "Hanover"
            ]
        }
]
}

Want I would like to receive back from the query are all the names in the municipalcities array for a given provincename.

Hope that helps, if you require the full document I can send it to you.

This is a screenshot from Postman

Thanks for your help.
Steven

Hi @Steven_Venter,

Thanks for the extra information.

If you would like to return back localmunicipalities.municipalcities you can project that field instead of localmunicipalities.municipality. As below example:

opts := options.Find().SetProjection(bson.D{{"localmunicipalities.municipalcities", 1}})
cursor, err := collection.Find(context.Background(), 
                               bson.D{{"provincename", "Northern Cape"}}, 
                               opts)

In addition, please note that municipalcities could be a typo of municipalities.

It looks like your application is a REST service, it would be great if you could limit the debugging scope. For example, try to debug just the function that perform the query.

If you still have further questions, could you isolate a smaller function scope and provide that as a reproducible example ?

Regards,
Wan.

Thanks for the update. I will try it and let you know.

Hi Wan

Unfortunately this does not solve the problem either. Only one object is returned and it is empty.

Hi @Steven_Venter,

I wasn’t able to reproduce this issue. It would be really helpful if you could create a standalone code repro that we can run locally. Also, when you say the object is empty, how are you accessing it? The Collection.Find function returns a Cursor and there’s multiple ways to iterate it, so it would be really helpful to have that code.

– Divjot