Query with multiple filter params and "or" condition

20 views
Skip to first unread message

RK

unread,
Oct 16, 2019, 11:40:19 PM10/16/19
to mongodb-go-driver
Hi,

I am trying to find collection using "or" clause with multiple filter options inside bson.M{}.
I would like to get collection of customers start with either first name or last name. 
But following code returning results after applying and clause. Can you please help with this code?

Thanks


filter
:= bson.M{}
if len(customerSearchRequest.FirstName) > 0 {
  filter
["customerprofile.firstname"] =  primitive.Regex{Pattern: "^" + customerSearchRequest.FirstName, Options: "i"}
}

if len(customerSearchRequest.LastName) > 0 {
  filter
["customerprofile.lastname"] =  primitive.Regex{Pattern: "^" + customerSearchRequest.LastName, Options: "i"}
}
cur
, err := collection.Find(context.TODO(), filter, findOptions)


Reply all
Reply to author
Forward
0 new messages