Querying first and last name with golang and mongo

329 Views Asked by At

I'm having some problems with querying the first and last name with go and mongo, basically if I search for "Jonh " (with the whitespace) it doesn't work and "John Doe" neither, but if I search "John" or "Doe" it works.

func (user User) Prepare() bson.D {
    var filters = bson.D{}
    var arrayFilters bson.A

    if user.Search != "" {
        arrayFilters = append(arrayFilters, bson.M{"profile.firstname": bsonx.Regex(user.Search, "i")})
        arrayFilters = append(arrayFilters, bson.M{"profile.lastname": bsonx.Regex(user.Search, "i")})

        filters = append(filters, bson.E{Key: "$or", Value: arrayFilters})
    }


    filters = append(filters, bson.E{Key: "deleteddate", Value: 0})

    return filters
}

I found some solutions with aggregate + concat but couldn't really translate it to bson

Mongo document:

 "_id": {
    "$oid": ""
 },
 "email": "",
 "profile": {
    "firstname": "john",
    "lastname": "doe",
 },
0

There are 0 best solutions below