Search Query with combination of two different fields
See original GitHub issueHello Dev, first of all thanks for the great work, and please consider I just started with watermelon DB and comes from Documents DB sides (Mongo etc.)
I have a very simple use case, but can’t find the exact answer for this
I have two fields in users modal named name and lastname and I want to match that fields with single search field, which usually used for user input in forms, so here is exact issue, I can search easily if there is a single word search by user, either first name or last name, i.e let assume the below use case
name : jhon
lastname: cena`
so that code works fine
data = await db.collections
.get('users').query(
Q.where('id', Q.notEq(user.id)),
Q.or(
Q.where('name', Q.like(`%${ customSanitizeLikeString(name) }%`)),
Q.where('lastname', Q.like(`%${ customSanitizeLikeString(lastname) }%`))
)
).fetch();
but here I assumed that name and last name should be only one word and split the input wich space but what is first name has multiple words like below
name : jhon cena
lastname: jhon papa
and I have to match the exact fullname or at least the all words entered, means exact first and last name or the maximum words as full name I know I can do it with with and instead of or but the issue is search box is single input fields and I don’t know where first name ends, and last name starts, so I have to do full name like search so what’s the best way to do so? as I don’t know which after which space last name start in input field?
what is used to do these like cases in mongo is to create a new fields one the fly (shadow fields )like full_name which combine both fields and perform search like this
.get('users').query(
Q.where('id', Q.notEq(user.id)),
Q.where('name', Q.like(`%${ customSanitizeLikeString(fullName) }%`)),
).fetch();
I didn’t found anything is watermelon db to combine fields like full_name on the fly like I do in mongoose, or i might missed some parts, and also suggest if there is any better way to achive the full name search without adding an extra fields as I don’t really wanna a duplicate field on creating/updating any document just for simple search purpose, thanks in advance
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)

Top Related StackOverflow Question
@raza2022 Hi, can you please tell me how did you connect WaterlemoDb with Reactotron ?
nope; I’d use a small snippet of SQL for this. Note that you don’t need to make the whole query a SQL, just this expression that runs LIKE on two concatenated fields.
BTW: I would say that it’s a bad practice to keep “first name” and “last name” as separate fields in the database, because, quite simply, not all names work like this… If you have any influence over this, I’d recommend that it is STORED concatenated. https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/