Saving And/Or Querying User Display Names in Firebase Using Caseinsensitive

Saving and/or Querying User Display Names in Firebase using caseInSensitive?

You can easily accomplish this task. We don't know how your current data is structured but here's an example

users
user_id_0
dsplay_name: "Smokey"
lower_case_name: "smokey"
user_id_1
display_name: "Bandit"
lower_case_name: "bandit"

When you create a user in Firebase, create a node in the /users node with their uid as the node name, and then display_name and lower_case_name as children

When you write your data to the users node, just lower case the display name when you are writing to the lower_case_name child:

let lowerCaseString = displayNameString.lowercaseString
let userRef = the users uid

let userData = ["display_name": "Bandit", "lower_case_name": lowerCaseString]
userRef.setValue(userData)

Then you query on the lower_case_name child using a lower case string.

ref.queryOrderedByChild("lower_case_name").queryEqualToValue("bandit")
.observeEventType(.ChildAdded, withBlock: { snapshot in
}

Is there a way to make a Firebase search query case insensitive without using child?

All queries in Firestore are case-sensitive.

If you need a case-insensitive mechanism you'll need to write a separate field that contains the case-insensitive version of the field and query against it. For instance:

db.collection("items").where("itemModel", "==", "Checkboard")
db.collection("items").where("lowercaseItemModel", "==", "checkboard")

There is a great answer from @DanMcGrath that I recommend you read:

  • Cloud Firestore Case Insensitive Sorting Using Query

Or an alternative from @samthecodingman:

  • Is there a way to search In Firebase firestore without saving another field in lowercase for case-insensitive search?

firebase query methods startAt() taking case sensitive parameters

There is currently no support for lowercase searches in Firebase. The best way to handle this would be store the lowercase string along side the original string and then query the lowercase string instead.

var ref_restMenu = firebase.database().ref()
.child('Restaurants')
.child('Company')
.child('menu');
var item = "Apple Pie";
// Or however you store data
ref.push({
itemName: item,
itemNameLower: item.toLowerCase(),
...
})

Then you could query as so:

//Check if item is already exist!
// query itemNameLoweruse and .toLowerCase()
ref_restMenu.orderByChild("itemNameLower").startAt(itemName.toLowerCase()).once("value", function(snapshot) {
var data = snapshot.val();
if(data !== null) {
//We will ger item name and restaurant id from this data.
callback(data);
} else {
//Item not found in globalMenu
console.log("%c Item not found in Global Menu", "color: red");
}
});

This does require replicating the data, but as of now there is not an easier foreseeable option.

Reference: Firebase Google Forum

Treating lowercase and uppercase as the same in database search

The answer is you store two sets of data in your node; one used for queries that's lowercased and the other used for display

users
uid_0
queryable: "mcdonald"
display: "McDonald"
uid_1
queryable: "van winkle"
display: "Van Winkle"

Searching in Firebase without server side code

Ok - there's no way to do exactly what you want with your current structure.

However this just popped into my head:

users:
user_1234
first_name: "Devid"
components:
"D": true
"e": true
"v": true
"i": true
"d": true
user_5678
first_name: "Andy"
components:
"A": true
"n": true
"d": true
"y": true
user_1010
first_name: "Bob"
components:
"B": true
"o": true
"b": true

and here's some ObjC Code to make it happen (and it's tested!)

Firebase *ref = [myRootRef childByAppendingPath:@"users"];

FQuery *q1 = [ref queryOrderedByChild:@"components/b"];
FQuery *q2 = [q1 queryEqualToValue:@1];

[q2 observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

NSLog(@"%@", snapshot.value);

}];

This code returns Bob.

To get all of the 'd' people, change the "components/b" to "components/d"

Edit:

You can get really crazy and add more combinations to expand your search capability

users:
user_1234
first_name: "Devid"
components:
"D": true
"e": true
"v": true
"i": true
"d": true
"De": true
"Dev": true
"Devi": true
"Devid": true
"ev": true
"evi": true
"evid": true
... etc

It would pretty simple to code up a few lines of code to iterate over the name and write out the combinations.

Obviously it would be way more efficient (if you have a limited data set) to just read all of the first names into snapshot, dump them into an array and (in ObjC) use an NSPredicate to pull out what you need.

Making a firebase query search NOT case sensitive

When using Firebase Database, that is the much you get to from orderByChild() function. It returns data either ascending or descending order and you cannot customize your result with extended queries.

However, You can try a something else that may be a bit more expensive.
Get all the children you want as an array, saving Child2 and its key. You can then change the string to upper case or lower case and access the desired result using the key.

The reference result would be

FirebaseDatabase.getInstance().getReference("Child1").child(key);

EDIT

To add on your logic, If you want "Dave" or "dave" to return Dave and dave
You can edit your query to startAt(text.toUppercase) and endAt(text.toLowerCase+ "\uf8ff"). This will return DAVE, Dave, dave etc

How to search Firebase text by lower case also?

There are two ways in which you can solve this problem. The first one would be to store the name of your items in your database in lowercase even from the beginning by using the method toLowerCase(). So if you want to search the names by lower case will work perfectly fine. Then if you want to dispay the names in your activity and you want to capitalize the first letter, then just use the following line of code:

String firstLetterCapital = lowerCaseItemName.substring(0, 1).toUpperCase() + lowerCaseItemName.substring(1);

So if you you have an item that has the name of item, using the above code, the result will be: Item.

And the second approach would be to store the lowercase name of your items as a property of your item like this:

Firebase-root
|
--- items
|
--- ItemIdOne
|
--- itemName: Item
|
--- itemToLowerCase: item

Then to create the search you can use a query that looks like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference itemsRef = rootRef.child("items");
Query query = itemsRef.orderByChild("itemToLowerCase").startAt(itemName.toLowerCase());

Firebase and Swift username case-sensitive

I think the fastest way with querying is to do this:

  1. Duplicate your username list in a new list where all your usernames are lowercased
  2. Lowercase the new username
  3. Check if that username exist in your duplicated list (where all usernames are lowercased)
  4. When it does not exists, append the real username without lowercase to the list with the usernames where they are case-sensitive and to the lowercased username list.

This duplicates data, but it is not that bad since it are only usernames.



Related Topics



Leave a reply



Submit