Like upper question, i want to get value of some field in firebase firestore instead of all document with DocumentSnapshot
like this in SQL SELECT col_1, col_2, col_3 FROM table_name
How can i do it?
Thank you for the help.
The Cloud Firestore client-side SDKs always read and returns full documents. There is no way to read a subset of the fields in a document.
You can retrieve the entire document, and then process the DocumentSnapshot to just use the fields you're interested. But this means you're using more bandwidth than needed. If this is a regular occurrence for your app, consider creating a secondary collection where each document contains just the fields you're interested in.
Also see Doug's answer here (for Swift): How to access a specific field from Cloud FireStore Firebase in Swift
Firestore can read single field value.
Firebase guides looks like didn't show these simple method.
For example:
[android]
String value = document.getString("col_1");
[node.js]
const value = doc.data().col_1
Firebase allows a few ways to query like an RDBMS database. And these are very handy too. Try the Where clause. It returns a single document but u can add multiple filters.
check for more
try this code:
//set a global variable
dataList: Array<any>;
const dataCollection = firebase.firestore().collection('data');
dataCollection.get(query => {
query.forEach(docs => {
this.dataList.push({id: doc.id, data:doc.data()});
})
})
Related
I am using this way to search for users in firestore database and I think that it is inefficient way , because it give unrelated results , for example if I type 'k' it give me results does not contain 'k' at all like the image below .
var query = firestore
.collection("users")
.where('name', isGreaterThanOrEqualTo: customer)
.get()
.asStream();
any suggestion please ?!
When you are using the following call:
.where('name', isGreaterThanOrEqualTo: customer)
It means that you are searching the database for the documents in which the "name" property holds a value that is greater than the one you are typing. In this particular case, both "Omar" and "Second Omar" are present in the result-set, since each one of them starts with a letter that is alphabetically ordered after "k". In fact, all capital letters are present in alphabetical order after the lower-case letters. Remember that the search is performed lexicographically.
If you are looking for a full-text search, please note that Firestore doesn't provide one. As #Dharmaraj mentioned in his comment, you should use a third-party solution. You can also find more info in my answer from the following posts:
Search by pattern on Cloud Firestore collection
Is it possible to use Algolia query in FirestoreRecyclerOptions?
I've been trying to find a query for almost 2 days now
I want to search id (current user id) from the document 4 fields (customer1,customer2,customer3,customer4)
Here is the firestore document picture
tried this query
final Query userQuery = collectionReference
.whereEqualTo("customer1",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer2",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer3",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer4",firebaseAuth.getInstance().getCurrentUser().getUid());
but this only shows up if the current ID is present in all 4. Is there any easier way to do this.
You can do that by using a field that is an array containing the uids you want to test, and then applying array-contains on it. In your case:
In your case:
customer: [customer1, customer2, customer3, customer4]
collectionReference
.where("customer ", "array-contains", firebaseAuth.getInstance().getCurrentUser().getUid())
Firestore does not support logical OR queries among mulitple fields. So, what you're trying to do is not possible with a single query using the database structure you have now. You would have to perform multiple queries and merge the results in the client.
If you want to be able to use a single query, you will have to change your database. One option is to put all the customers into a single array field and use an array-contains query to find a customer in that field.
whats up?
I have an app that displays a list of items on Firestore using Kotlin and RecyclerView (from FirebaseUI api).
The DB is structured like this:
Users/userID/document_month_year
I need to query the data from the current user.
Each user has his own document_month_year document.
I read a lot of posts here, but each one tell one thing.. thereĀ“s no consense and nothing seems to work.
This query just sends me all documents from all users, how can I fix this?
private val queryFilteredByPaidStatus = db.collectionGroup(collectionName).whereEqualTo("users", userId)
Like this is an important question, here is the awnser that I
private val queryTest = db.document("users/"+userId).collection(collectionName)
fun getData() : FirestoreRecyclerOptions<Expense> {
return FirestoreRecyclerOptions
.Builder<Expense>()
.setQuery(queryTest, Expense::class.java)
.build()
}
Create a separate collection for documents to be read i.e month_year and for each document, add a field inside it which tells you the uid of the authenticated user, to which the document belongs to. Now you can query the collection like:
firestoreDB.collections("month_year").whereEqualTo("uid",auth.currentUser.uid)
This is my first post here so sorry for any confusion!
I recently started using Flutter and I'm new to Firebase too.
I'm using a realtime database as the backend for my app, and I've got a hierarchy that looks like:
What I want to do is lookup a specific user by their id, fetch their information (gender and name in this case) and then set a widget (in this case a text widget) to use that value.
How can this be accomplished?
I've searched through a lot of StackOverflow questions about this, and all of them use FutureBuilders or DataSnapshots and I can't quite work out how to go about sorting them out so they work.
All of the tutorials online also don't get info from a specific user and instead query the entirety of the database.
You can do the following:
db = FirebaseDatabase.instance.reference().child("Users");
db.orderByKey().equalTo(Id).once().then((DataSnapshot snapshot){
Map<dynamic, dynamic> values = snapshot.value;
values.forEach((key,values) {
print(values["name"]);
});
});
First, you get an instance to the node User, and then using orderByKey() you can search the user by the id, and retrieve the name and gender.
Check the link following for more information:
https://github.com/flutter/plugins/blob/master/packages/firebase_database/lib/src/query.dart#L183
I am implementing Firebase Realtime Database for my app.
The database contains a lot of entries that will continue to grow in the future.
I want to retrieve only a specific child from the database, but i can't find any implementation for doing that.
In Firebase too the implementation retrieves all the entries from the database.
This will be cumbersome in the future.
Is there any way to retrieve only specific entries or just one entry(depending upon the customisation)?
Please help.
#ALex Mamo: This is the database image
Perhaps you are refering to shallow query, which only read the first child ? In this case you might want to look at the API implementation as mentioned here:
Firebase (2016) Shallow Query
There are two different ways of getting it. You'll have to change 'messages' to the name of your database. Replace PostTitle with the specific data you're trying to receive.
var rootRef = firebase.database().ref().child("messages");
rootRef.on("child_added", snap=> {
var postTitle = snap.child("PostTitle").val();
or
exports.sendPushNotification = functions.database.ref('/messages/{id}').onWrite((change, context) => {
const afterData = change.after.val();
const postTitle = afterData.PostTitle;
if you want retrieve few records this might help. as per image structure attached.
you can restructure the data, and use set method with keys as 0,1,2.... instead of "LK34h...."->0 .
use set method and store latest index value.
you retrive like this
firebase.database().ref().child('users_Post/0').on('value',sn=>{ console.log(sn.val())//use sn.val() })