Giving the UID for the document's name - android

I would like to know what is the best practice to save users preferences in my firestore database.
I would try to explain with an example...
Case 1
I have this kind of Document in my "users" Collection (the name is random generated by Firebase) with 3 fields :
user_uid : String
nickname : String
android_lover : boolean
In my Android project, when I want to search the Document of the user "DFDDE45554SDC", I search where user_uid = "DFDDE45554SDC".
Case 2
I have this kind of Document in my "users" Collection (the name is created with the UID of the user) with 2 fields :
nickname : String
android_lover : boolean
In my Android project, when I want to search the Document of the user "DFDDE45554SDC", I just search the Document "DFDDE45554SDC".
I specify : I don't want duplicate users.
So, what is the best practice (security, optimisation,...) ? Why ?

I would suggest that Case 2 is more effective, for a few reasons:
We already know the user's ID, so don't need to use a different ID here.
Using usersCollection.document(userId) is simple to construct and is a direct DocumentReference, rather than a Query, therefore:
A DocumentReference can be stored in the Firestore database, whereas a Query cannot.
A DocumentReference would likely scale better than instructing the Firestore database to perform a filter query using whereEqualTo("user_uid", userId) (although with indexing, the performance difference is likely negligible at this point).
A Query will always return a collection of results (even if there is only 1), rather than the exact document.
There isn't currently a need for a different randomly-generated ID for each document within the users collection because the user ID is already unique.
You only need 1 document for each user, so this is a sure-fire way to ensure there won't be any duplicates.
The only real incentive I can think of to use Case 1 would be to standardise your document naming scheme with other collections in your database, but this doesn't really matter so much with Firestore.
For a quick example of the two in Android:
Case 1
db.collection("users")
.whereEqualTo("user_uid", "DFDDE45554SDC")
.limit(1)
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
// Even with limit(1), we still receive a collection
// so iterate this to obtain the desired document
}
}
}
});
Case 2
db.collection("users")
.document("DFDDE45554SDC")
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful() && task.getResult() != null) {
// We have access to the single desired document directly
DocumentSnapshot document = task.getResult();
}
}
});

Related

Check if a field exists - search through Firestore database

I am doing a project for school - android app which registers users to realtime database after it checks if there's a corresponding card number and phone number in a different database in Firestore. At the moment it verifies only the first document, but it wouldn't find the fields if I search for them in other documents.
This is the method I use:
public void checkIfCardExists() {
Query query = cardInfo.whereEqualTo("CardNo", cardNumber)
.whereEqualTo("Phone", userPhone);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
boolean documentExists;
if (task.isSuccessful()) {
Log.d("QueryResult", "Is query result empty: " + task.getResult().isEmpty());
documentExists = !task.getResult().isEmpty();
}else {
Log.e("QueryResult", "Error getting documents.", task.getException());
documentExists = false;
}
if(documentExists) {
Log.d("QueryResult", "The document exists");
Toast.makeText(com.example.transportticket.RegistrationLeap.this, "Card number found",
Toast.LENGTH_SHORT).show();
userLeap = new UserLeap(userEmail, userPass, userName, userSurname, cardNumber, userPhone);
registerUserLeap(userEmail, userPass);
startActivity(new Intent(RegistrationLeap.this, Empty.class));
}else{
Log.d("QueryResult", "The document doesn't exist or there was an error retrieving it");
Toast.makeText(com.example.transportticket.RegistrationLeap.this, "Card number not found",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegistrationLeap.this, Empty.class));
}
}
});
}
And this is how my Firestore database looks like
Firestore database
I added a photo to clarify about finding the first document
If you are using the following line of code:
Query query = cardInfo.whereEqualTo("CardNo", cardNumber)
.whereEqualTo("Phone", userPhone);
It means that you are telling Firestore to return all documents where the CardNo property holds the value of cardNumber AND the Phone property holds the value of userPhone. So if in your collection only one document satisfies this constraint, a single document will be returned. The other documents won't exist in the results. What you are doing now, is called filtering. However, if you want to get all documents, then you should remove both whereEqualTo() calls or directly use cardInfo which is a CollectionReference object. In this way, you aren't filtering anything. A CollectionReference object is basically a Query without any filters.
So using the last solution you can get all documents and you can also create the filtering on the client. This is not a recommended approach because getting all documents, will be very costly. For instance, if you have in your collection 1000 documents, you'll pay 1000 read operations to have them. So it's up to you to decide which one is better for you.

Android Studio: Query documents containing array node using a substring

I am trying to search Documents in a Collection which contain a certain key.
Here is how I structured my Firestore database:
-- FirestoreRoot
|-- Products (Collection)
|-- Departments (Document)
|-- Food (Colletion)
|-- {Id} (Document)
-- description : "this is my very first description"
-- keywords :
-- 0 : this
-- 1 : is
-- 2 : my
-- 3 : very
-- 4 : first
-- 5 : description
In the example below, I was able to search by using a substring for the first word in the description. To this date, this method does not work for querying the following words. In the example below, typing the letters "thi" is enough to return documents.
CollectionReference colecRef = FirebaseFirestore.getInstance()
.collection("Products")
.document("Departments")
.collection("Food");
Query query = colecRef;
query.whereGreaterThanOrEqualTo("description", searchField.getText().toString().toLowerCase())
.whereLessThan("description", searchField.getText().toString().toLowerCase()+'\uf8ff')
.get().addOnSuccessListener(SearchActivity.this, new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Log.i(TAG, "onSuccess: "+documentSnapshot);
}
}
});
In my case, I need to search by using 1 or multiple keys, for example: first or first description. And by using the whole word or only a substring, for example: first or descr
I've tried to use whereArrayContains(), but I can't use it typing multiple keys or substring.
query.whereArrayContains("keywords",searchField.getText().toString().toLowerCase())
.get().addOnSuccessListener(SearchActivity.this, new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Log.i(TAG, "onSuccess: "+documentSnapshot);
}
}
})
I appreciate any help.
Firestore currently only offers two ways to query the contents of an array:
Array membership with whereArrayContains(). This will tell you if an element exists in an array. It must match exactly - no substrings.
array-contains-any memberhip with whereArrayContainsAny(). This will tell you if any of the given strings exist in the array. It must match exactly - no substrings.
As you can see, searching substrings in arrays isn't going to work at all. You might want to consider using another database in tandem with Firestore in order to satisfy these specific queries, as Firestore is not very well suited for them.

How to retrieve and display data from users that are logged in - Firestore

I am trying to get and display my user's information when they are logged in. (i.e: name, email, phone)
I have tried multiple snippets i have found on youtube and on stack overflow but they have failed. Most tutorials use realtime Database, which is not what i am looking for.
I have also tried making a "users" object.
private void getData(){
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users")
//.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("email:", FirebaseAuth.getInstance().getCurrentUser().getUid())
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
//Toast.makeText(getApplicationContext(),document.getId() +"==>" + document.getData(),Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(),""+ document.get("Email") ,Toast.LENGTH_LONG).show();
nameEdt.setText((CharSequence) document.get("First Name"));
emailEdt.setText((CharSequence) document.get("Email"));
phoneEdt.setText((CharSequence) document.get("Phone"));
}
} else {
Toast.makeText(getApplicationContext(),"No such document",Toast.LENGTH_LONG).show();
}
}
});
}
Database Structure:
I understand that documents in firestore are not associated with users, but i dont know how to set my code up so that it only retrieves data from the user that is signed in* It works fine for newly created accounts, but if i were to log out and sign in with a different user it will not update the "account/user information".
In short, how would I access and display my database information from signed in users?
Additional Notes: I am using Email and Password for authentication
To access your user data stored in Firestore, it shouldn't be as complicated as you thought, there's no queries needed, you just need to fetch the documents corresponding to the user's uid, and fetch the specific fields or do whatever you need with them, like this:
db.collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.get().addOnCompleteListener(task -> {
if(task.isSuccessful() && task.getResult() != null){
String firstName = task.getResult().getString("First Name");
String email = task.getResult().getString("Email");
String phone = task.getResult().getString("Phone");
//other stuff
}else{
//deal with error
}
});
Original Answer:
User information is not stored in the Firestore database, they are associated with the Firebase Authentication which you set up for the log in. To retrieve the related user information, you need to use the related FirebaseAuth APIs. Use this to retrieve the current log in user:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Then you can get the name and email with something like this:
String name = user.getDisplayName();
String email = user.getEmail();
For more information, refer to the documentation.
If FirebaseAuth doesn't resolve, that probably means you didn't follow the set up guides correctly and forgot to include the dependency in your gradle file:
implementation 'com.google.firebase:firebase-auth:17.0.0'
After a couple days head butting at trying to find a solution, i have found one that is able to retrieve user information from the database. However it is important to note that because my application is not holding a lot of data so this structure works for me.
So i was essentially on the right track, but with some lack of understanding of firebase i missed a few concepts.
private void getData(){
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String current = user.getUid();//getting unique user id
db.collection("users")
.whereEqualTo("uId",current)//looks for the corresponding value with the field
// in the database
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
nameEdt.setText((CharSequence) document.get("firstName"));
emailEdt.setText((CharSequence) document.get("email"));
phoneEdt.setText((CharSequence) document.get("phone"));
// These values must exactly match the fields you have in your db
}
}
As mentioned before, documents do not associate with users, but you CAN link them together by creating a field in your db called "whatever your want" (i made mine uId). This is because firebase generates a unique id for each user when authenticated. By creating a field that holds that unique id you are able to retrieve the associated information in that collection.
How to create the field:
I created a "user" object that would grab the uid from my edit text. In my code, i passed the uid wherever i was creating/authenticating a new user/account.
FirebaseUser testUser = FirebaseAuth.getInstance().getCurrentUser(); //getting the current logged in users id
String userUid = testUser.getUid();
String uidInput = userUid;
User user = new User(firstNameInput,lastNameInput,uidInput);
db.collection("users").document(userUid)
.set(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
note: I believe you can also add it to your hash map if you have it done that way.

Android Firestore query get the id of the document that contains the value in the search

Firestore database image
Hello, I just tried to use Firestore. I had some problem when getting document id.
The question is, I want to get a document id (red box) which has value (blue box) in it.
I use the following query:
collection("mychannel").whereEqualTo("74wRU4xHrcV9oWAXEkKeRNp41c53")
But did not give results.
Thanks!
As in the official documentation:
Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.
So there is no way in which you can use the following query:
collection("mychannel").whereEqualTo("74wRU4xHrcV9oWAXEkKeRNp41c53")
If you only want to get the entire userId array you need to iterate over a Map like this:
collection("mychannel").document("1fReXb8pgQvJzFdzpkSy").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Map<String, Object> map = document.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals("userId")) {
Log.d("TAG", entry.getValue().toString());
}
}
}
}
}
});
But note, even if userId object is stored in the database as an array, entry.getValue() returns an ArrayList, not an array.
So the output will be:
[74wRU4xHrcV9oWAXEkKeRNp41c53]
A better approach will be if you consider this alternative database structure, where each user id is the key in a map and all values are true:
userId: {
"74wRU4xHrcV9oWAXEkKeRNp41c53": true,
"AdwF...": true,
"YsHs...": true
}
This question is answered here: Firestore: Query by item in array of document
In summary, don't use arrays to store data in Firestore as the query you are trying to do is not available yet (remember it is still in beta). You should use a Map instead.

How can i read ArrayLists within a document from Firestore cloud database?

I'm Trying to build a restaurant app using firestore to store the orders and the users.
I tried 2 methods, first one was to write the ArrayList of orders as a Array in firestore database, but coudn't read them afterwards...
I used DocumentSnapshot.toObject(myclassoflist.class) but this only worked with no array list in the document, only with values = ".."
Then I created a collection of documents (each document is an item) which contains the array as simple values
To understand this take a look at my database
Then, to read them i first get all the document ids
db.collection("orders").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
documentsIDs.add(document.getId());
}
Integer allIdsD = task.getResult().size();
if (allIdsD.equals(documentsIDs.size())) {
readDocs();
}
}
}
});
Then for each document id, i created 3 more db.collection(collection).get() in order to get the inside of each document within the subcollection, using again the function DocumentSnapshot.toObject(myclass.class).
The problem here is that it takes ~0.8 secs to get a complete order from the database, which is a lot considering there could be like 100+ orders per day
My project on GITHUB
Examples from: LimatexMM/app/src/main/java/g3org3/limatexmm/orders.java
EDIT:
I also tried to write the orders as follow:
orderListBig docData = new orderListBig(list, currentUser, adList, docId);
(list is ArrayList, currentUser, adList are objects)
db.collection("orderss").document(docId).set(docData)
and then read it with:
db.collection("orderss").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
orderListBig ahah = document.toObject(orderListBig.class);
allOrders.add(ahah); (ArrayList of ordersListBig)
an part of my order document
As per official documentation regarding the use of arrays in Cloud Firestore you need to know that:
Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.
If you only want to get the entire orderList array and get the value of, let's say itemMore, you need to get the reference of that particular document and then iterate over a Map like this:
Map<String, Object> map = documentSnapshot.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals("itemMore")) {
Log.d("TAG", entry.getValue().toString());
}
}
But note, even if orderList object is stored in the database as an array, entry.getValue() returns an ArrayList, not an array.
I better approach for your use-case, would be if you consider this alternative database structure, where each item is the key in a map and all values are true:
orderList: {
"itemMore": true,
"itemMoreValue": true,
"itemMoreOtherValue": true
}
Bafta! ;)

Categories

Resources