0

I've been learning go and I am able to work with some basic MongoDB functions via the mongo-driver I am unable to figure out the search functions. I believe this is due to the interpreter adding quotes around my search.

    searchQuery := "/" + params.Get("q") + "/"

    filter := bson.D{
        {"$and",
            bson.A{
                bson.D{
                    {"title", searchQuery},
                },
            },
        },
    }

cur, currErr := collection.Find(ctx, filter)

I am expecting to add a partial string to the query string and have return all of the documents that match. If I could query the search index i've added that would be even better. Either way I am unable to get it to work.

Alan B
  • 11
  • 1
  • I should note that if I remove the "/" from the param exact matches work. – Alan B Apr 05 '22 at 19:52
  • Have you tried looking at the documentation for mongodb? Why are you adding `/`, and why are you using `$and` if you want to match a partial string? – super Apr 05 '22 at 20:04
  • @super I'm trying to find a partial of the title using the `/search/` syntax. I think that Go is escaping the string or adding quotes. Even when I output the filter, I'm not seeing the issue. – Alan B Apr 05 '22 at 20:39
  • Please do check the marked duplicates. Go does not escape the term, but you can do explicitly. Also case matters, you have to tell explicitly if you want case insensitive search (see marked duplicate). – icza Apr 05 '22 at 20:50

0 Answers0