Firestore multiple querys on the same field [duplicate] - firebase

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

Related

Retrieving a list of entires in Google Firebase's Firestore [duplicate]

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.

Query Document Based on Regex in Firestore [duplicate]

This question already has answers here:
Query over list in firebase
(2 answers)
Closed 2 years ago.
I want to query a document in Firestore using a regex. This is because in order to add a search functionality into my Flutter app, I need to be able to create a query based on the search. For example if the user types: 'alice' -- I would have a regex that would look like (?i)alice and I would query firestore for any documents that the name field meets this regex, whether it be Alice, Alice Doe, or Doe Alice. I'm looking for a query that would look something like this:
Firestore.instance.collection('people')
.where('name', matchesRegex: '(?i)alice')
Thanks for any help. I feel like this is a pretty basic feature but I can't find it any where in the docs. And I really can't get all of the documents and search based off of that because the collection is very large, and I don't want to read that many documents.
Firestore does not offer any regular expression in queries. Those types of queries do not scale in the way that Firestore requires. It only supports exact string matches, and range queries with string prefixes.
If you need more flexible text-based queries, consider mirroring your data to a text search engine, such as Algola, as describe in the documentation.

Check if an event is ongoing [duplicate]

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.

Firestore sub collection querying data [duplicate]

This question already has answers here:
How to list subcollections in a Cloud Firestore document
(4 answers)
Closed 5 years ago.
I am reading the docs and see that FireStore allows sub-collection. Which is great. Consider the following example as mentioned here int he docs.
As shown in the docs I can get the reference to the last doc as follows
var messageRef = db.collection('rooms').doc('roomA')
.collection('messages').doc('message1');
In the above example, the id's for docs and collections are typed in.
There are cases when id's are dynamically generated. In such a case how can I know how many collections a doc has? Or whether a doc has any sub-collections. How can I do that?
On mobile clients, you can't know how many collections or what collections a document has. There is just no API for that. You have to know ahead of time what collections may exist for a document, or accept that a query on an unknown subcollection may return no documents.

Retrieve totally different keys from Firebase [duplicate]

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..

Resources