I have an array of ingredient names that is dynamic and provided per user. I'd like to match it to mongo
documents where there is an array
of objects called ingredients
which has a property name
. I've written a query (see below) which will take query parameters from the URL and will return all the documents that have all matching ingredient names, however this search is case sensitive and I'd like it not to be.
I've considered using bson.RegEx
with Option: "i"
, however I'm not sure how to form this query or apply it to an array of strings.
Here is the case sensitive query:
// Check for ingredients, return all recipes that can be made using supplied ingredients
if qryPrms["ingredients"] != nil {
mongodQ["ingredients.name"] = bson.M{"$all": qryPrms["ingredients"]}
}
mongodQ
is the bson.M
I use to query the collection later in the code. Ideally I could apply RegEx
to each element in qryPrms["ingredients"]
so it would return closely matching ingredients like cheese
would return swiss cheese
as well. This is also a more general mongodb
question I suppose when it comes to querying with a dynamic array.