This question already has an answer here:
firebase equivalent to sql where in ()
(1 answer)
Closed 5 years ago.
I use orderByKey for requesting data by keys from Firebase. I can get several objects if keys are similar — start or end with the same string.
But how can I retrieve totally different keys with one request to Firebase, e.g. "n:1-2-3" and "n:2-3-4"? I use equalTo, but can I specify more than one key there?
I know how to do it with different requests — one request per one key, but it's not optimal.
Currently firebase only supports one condition. Explained more in the below text:-
If lets say you have two keys that are a123 and a234 then to retrieve you can do this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
ref.orderByKey().startAt("a").limitToLast(10).addValueEventListener(..){..}
The limitToLast() method is used to set a maximum number of children to be synced for a given callback
You can use limittolast or limittofirst, to limit the results that are obtained from the database.
Also currently firebase only supports one condition. So you cannot have more than one orderbykey or orderbychild or the two together..
Related
This question already has answers here:
Firestore - get specific fields from document
(4 answers)
How to access a specific field from Cloud FireStore Firebase in Swift
(5 answers)
How to get a list of document IDs in a collection Cloud Firestore?
(4 answers)
Closed 1 year ago.
Suppose I wanted to use Google Firebase's Firestore as the backend of a simple website showing a list of (journal) entries, and the ability to view an entry, edit an entry, and delete an entry. Fundamentally, I would want to retrieve the list of entry titles, so I could present in my website a table of contents. Then when the visitor clicks one of the titles, the website would navigate to the entry's content.
Now, my question is, how do I get just the titles without the content, of the entries? As far as I have read, this is not possible. I present this problem here to confirm whether I have missed something, or if it is indeed impossible with Firebase to get some of the data from a collection of records, without having to retrieve all of the data.
how do I get just the titles without the content, of the entries?
As you've already found, the client-side SDKs for Firestore always retrieve full documents. The server-side SDKs and REST API have a projection/selection mechanism to retrieve only a subset of the fields, but that ability does not exist in the client-side SDKs.
This question already has an answer here:
Between StartDate and EndDate Firestore
(1 answer)
Closed 3 years ago.
I have a collection of events in my Firestore database where each one has a startDate and an endDate. On client side I would like to query for events that are happening right now. Normally, you would just check if currentDate is inside the time interval, however this can't be done with Firestore since it is disallowed to have more than one comparison query.
Has anyone encountered this problem and how did you overcome it?
I you are storing the startDate and endDate as milliseconds, you could just take your client time in milliseconds and make a query against your events to know if it has already started, is ongoing or is done.
Personally I usually store dates in millis to make queries and also in a human readable format when I check the documents using the Console.
however this can't be done with Firestore since it is disallowed to have more than one comparison query
No, you can query a Firestore collection using multiple comparison functions but only on the same property and not on multiple properties as you intend to do. According to the official documentation regarding query limitations:
Query limitations
Cloud Firestore does not support the following types of queries:
Queries with range filters on different fields, as described in the previous section.
So as you have probably noticed, Cloud Firestore allows a range filter on a single field. The most reasonable explanation is that Firestore cannot guarantee its performance in such a case.
To solve this, you'll have to query your collection twice and combine the results of those queries on the client. It's not perfect since you need to query twice, but I think it will do the trick.
This question already has answers here:
Firestore multiple range query
(3 answers)
Closed 4 years ago.
We are building a app where we need to show nearby posts, made by users. A post should only be shown, if it is not more as 4 hours since posting. Because firestore doesn't provide geoquerys, i have to do a search, where all posts in a specific square are returned. This is my current query:
collection("posts")
.where("creation", isGreaterThan: DateTime.now().subtract(Duration(hours: 4)))
.where("location", isLessThan: cornor1OfSquare)
.where("location", isGreaterThan: cornor2OfSquare)
The problem with this query is, that it's not allowed to add multiple where statements filtering on different fields.
Filtering by one property locally and one on the server side is not possbile, because we have millions of posts in our app.
Is there any other way to do this? For example restructure the data on the server, to make queries like this possible? I researched, but only found solutions, with "simple" data. But i have a Timestamp and a GeoPoint field, which makes it more complex.
The second approach, which comes to my mind, is to use cloud functions, but i have no idea how to do that, and if it is the best solution available.
I am programming in Dart/Flutter.
The Cloud Firestore doesn't support range filters on different fields.
You most create separate query for each OR condition and merge the query results.
See this link for more: https://firebase.google.com/docs/firestore/query-data/queries?hl=en-us
This question already has answers here:
Firebase Query Double Nested
(3 answers)
Closed 4 years ago.
I have the following data structure:
items:
id1 - dynamically set, unknown
id2 - dynamically set, known
item: data
I want to get all data inside id2.
Using firebase.database().ref('items').orderByChild('item').equalTo('data') I get null but at the same time it's no problem to access all data inside id1 though. Apparently using this method it's impossible to access data with 2 and more levels of depth.
Firebase declares they support up to 32 levels of nesting. So there should be some methods for advanced searching data anyways.
How can one access an object with known property and known value while searching at 2 or more levels of depth with arbitrary keys?
Unfortunately firebase realtime database gives you the ability to filter children only one level deep. If you have the flexibility to reach the specific id1(which you declared unknown), you can filter with orderByChild and equalTo method for item by going down the hierarchy. Alternatively you can filter manually client side or change your data structure if you have the chance.
From the firebase reference:
Filter by key or value
You can use startAt(), endAt(), and equalTo()
to choose arbitrary starting, ending, and equivalence points for
queries. This can be useful for paginating data or finding items with
children that have a specific value.
https://firebase.google.com/docs/database/web/lists-of-data
This question already has an answer here:
How to create auto incremented key in Firebase?
(1 answer)
Closed 2 months ago.
How can I get next key value (User03) continue from last recent key (User02) like image below:
To know the next key in this sequence, you will need to first load all the current keys.
This is one of the many reasons Firebase recommends against using arrays, since such an operation doesn't scale well, and will only work while the user is connected to the database. I recommend reading best practices for arrays in Firebase and what are Firebase push IDs.