question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Search Query with combination of two different fields

See original GitHub issue

Hello 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:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
vishgoel007commented, Dec 28, 2020

@raza2022 Hi, can you please tell me how did you connect WaterlemoDb with Reactotron ?

1reaction
radexcommented, Sep 14, 2020

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/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Specify Multiple Search Conditions for Multiple Columns
To create a query that searches for values in either of two (or more) columns, you specify an OR condition. To create a...
Read more >
search in combination two field in Mongodb - Stack Overflow
To search against a combination of two or more fields, you need to use the aggregation framework. It lets you sort by the...
Read more >
Combined fields | Elasticsearch Guide [8.5] | Elastic
The combined_fields query supports searching multiple text fields as if their contents had been indexed into one combined field. The query takes a ......
Read more >
SOQL Query on combination of two fields - Greytrix
Now, we can use this newly created field to query on this object to get the required records. New SOQL query will be,...
Read more >
Use multiple fields in a calculation | InfluxDB Cloud ...
Query multiple fields, pivot results, and use multiple field values to calculate new values in query results.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found