Firebase realtime database returns only one entry - firebase

I am currently trying the firebase realtime database and I am executing my first queries. I am using the Firebase Admin console for querying.
I now have a problem that I only get one entry back by a query when using ordering. My data structure looks like the following:
The query I am using is:
firebase().database().ref('/Office1/Department1/HR').orderByChild('Rank').on('value', function (snapshot) {
snapshot.forEach(function(childSnap){
console.log(childSnap.val());
});
})
As an output I only get:
{"Name":"Charles","Rank":1}
As you can see the ordering works, however it only shows one item instead of all 3 entries.
Somebody knows how to fix this issue?
Thanks

Related

How can I get data from only one table in Firebase Realtime Database

I have a big problem that I need to address quickly.
I have my firebase realtime database structured this way:
Picture:
My question is simple:
How can I only retrieve the friendIds as a list without the inner data i.e firendsStatus, lastMessage, lastTimestamp...
My current response is like this: {friendsArray: {'34uibjerktbbt854uigefg' : {'lastMessage' : 'ajskdkjasd',...}, {'574uibsd784546ktbbt854uigefg' : {'lastMessage' : 'weurhisdf', ...} and so on}}
I want it to be like this: {friendsArray: ['34uibjerktbbt854uigefg', '34uibjerktbbt854uigefg', '34uibjerktbbt854uigefg', and so on]}
Is there any way to achieve this because if I observe the whole data it gets pretty big and slow?
Thank You
Thats unfortunately not how realtime database works it always fetches all of the data below as well, you can use cloud firestore instead or re-design your realtime database so the nodes are not tested you can create separate nodes that only contains the data you need to fetch

Flutter Firebase documents are randomly ordered

I made a simple chat app using Flutter. My app is functioning perfectly fine (getting and dumping data into firebase) but once my messages get into my database, they are randomly ordered resulting in my msgs getting displayed in a random order.
I have tried reversing the SnapshotQueue in my Flutter code but that did not help...
You will need to attach document names to your documents before uploading them.
I think you are experiencing this issue because your documents are being given Auto Ids.
Try using the current timestamp as the document name. this will help arrange documents in order according to time uploaded.
Firestore.instance.collection(CollectionName).document(Timestamp.now()).setData(messageMap);
i hope this is what you need. if not. Please share your code that uploads the message to database.
Add a field like datePublished = DateTime.now() to each message document as
it gets created to
store the timestamp of when the message was created
Then you can use datePublished field to order your documents of your QuerySnapshot like this:
QuerySnapshot snapshot = await collection.orderBy('datePublished', descending: true).get();
now your messages are arranged in chronological order from the latest
to earliest.

Flutter Profile Matching App - Cloud Firestore Data Modeling | How to Query to avoid unnecessary reads for already swiped UID´s

I tried my best with the search function and not really getting the results I´m searching for.
At the moment I try to replicate a functionality for swiping profiles on a card stack. The profiles are loaded over a Firebase Firestore backend. The problem is, due to the geoFireStore query, I get with every load within a specific radius a whole bunch of documents and I would like to reduce the read amount directly in the query.
So what I'm trying to achieve is, that when I swiped a profile once (left or right) I never want to read it in the query again. In addition, to save reads, I don't want to do this client sided, it should be done in the query directly.
Firestore JSON structure at the moment:
Users (collection)
UIDs [documents]
name
active
match {map}
If the match map doesn't contain the own UID (if exist is null) then read in query, else when own UID exist in other user profile under the map match (boolean: true or false for liked or disliked) then don't show as query result.
My stream is built this way:
return geo
.collection(
collectionRef:
friendrCollection.where("active", isEqualTo: true).where("match.$uid", isNull: true))
.within(
center: geoFirePoint,
radius: suchRadius,
field: 'position',
strictMode: false)
.map(_friendrsGPSListFromSnapshot);
}
I am working on this for 3 weeks now and not getting the results I want :D
-Should I stay with firestore or better the real-time-database?
-How do I need to structure the data to get a "except" query working?
Right now it is like:
Collection: Users --> Documents: UIDs --> Match{ownUID}
Thanks in advance
Andreas :)
I still not managed to find a proper solution to reduce the number of unnecessary profile reads inside the firestore query.
Has anyone an idea to handle this part?
The only way I found was to lookup existing matches on the client side (extra firestore collection), but that means that I always have to load the whole bundle of profiles and throwing them away afterward :(
Thank you!
Andreas

Use Firestore autoID to retrieve and display data in Flutter

im new to flutter and dont know exactly what to search for this one but, can we use the auto generated ID like in the picture to retrieve all that data UNDER that ID? if so, how ? In a similar question that I stumbled upon, they use database.reference() but its a Realtime Database and not FireStore
Im using Firebase Cloud Firestore
There is no AutoId in firebase but here is a quick way to set as auto id
Forexample ;
1- create yourModel ( which one u gonna send as model to firebase )
2- DatabaseReference firebaseDatabase;
3- firebaseDatabase =FirebaseDatabase.instance.reference();
4- firebaseDatabase.child("table_name").push().set( yourModel.toJson() );
Also for getting data u can write code like that
var result= firebaseDatabase.child("table_name").once().then(
(DataSnapshot datasnapshot){
Map<dynamic,dynamic> values= datasnapshot.value;
values.forEach((key,value){
print("key:"+key+" value:"+value["name"]);
});
}
);
print(result);
I tried it and works great
Have a nice day !!!
I'm guessing you're asking about subcollections.
If you read a document (by its (auto-generated or not) key), you get back that document. You don't get back data from any subcollection. That will require a separate read operation for each subcollection under the document that you want to read.

Can't get Auto-ID Document in Firestore

Excuse me,
I'm working on my code about making a Document Firestore that could be delete, this is how my Firestore Database looks like :
Firestore Data Structure
I'd like to delete the Auto-ID Document after the Collection "X-MIPA.6". So, I followed the previous solution about "Firebase Cloud Firestore get auto-id of newly created document", so here is my code looks like :
My Code
But, when I tested, my app force closed and show me this Error :
The Error
How is it possible? How can I fix it?
I want to delete the Document based on the Auto-ID Document on my Firestore Database.
Thanks!

Resources