Firestore Query Documents Startswith a String

Firestore query documents startsWith a string

You can but it's tricky. You need to search for documents greater than or equal to the string you want and less than a successor key.

For example, to find documents containing a field 'foo' staring with 'bar' you would query:

db.collection(c)
.where('foo', '>=', 'bar')
.where('foo', '<', 'bas');

This is actually a technique we use in the client implementation for scanning collections of documents matching a path. Our successor key computation is called by a scanner which is looking for all keys starting with the current user id.

Firestore query documents startsWith a specific character

From the top of my head, if interested in the letter "I":

var lower = 'I';
var upper = String.fromCharCode(lower.codeUnitAt(0) + 1);

itemRef
.doc(this.itemsId)
.collection("october")
.where(firebase.firestore.FieldPath.documentId() ">=", lower)
.where(firebase.firestore.FieldPath.documentId() "<", upper)

Not sure, what happens in case of the letter Z.

Firestore Flutter Searching : query documents starts with or equals to a string

The solution is to use 'isGreaterThanOrEqualTo' combined with 'isLessThan' and char codes :

return FirebaseFirestore.instance
.collection(Strings.usersCollection)
.where('userName', isGreaterThanOrEqualTo: query, isLessThan: query.substring(0, query.length-1) + String.fromCharCode(query.codeUnitAt(query.length - 1) + 1))
.get();

Is there an equivalent to `beginsWith` in Firestore?

[Googler here] You are correct, there are no string operations like beginsWith or contains in Cloud Firestore, you will have to approximate your query using greater than and less than comparisons.

You say "it doesn't exist because the performance would be terrible" and while I won't use those exact words you are right, the reason is performance.

All Cloud Firestore queries must hit an index. This is how we can guarantee that the performance of any query scales with the size of the result set even as the data set grows. We don't currently index string data in a way that would make it easy to service the queries you want.

Full text search operations are one of the top Cloud Firestore feature requests so we're certainly looking into it.

Right now if you want to do full text search or similar operations we recommend integrating with an external service, and we provide some guidance on how to do so:
https://firebase.google.com/docs/firestore/solutions/search

Firestore get document that contains a string

There is no way to search for documents whose ID contains a substring, nor for those whose ID ends with a substring. The only possibility is to find documents whose ID starts with a substring.

You will instead have to maintain a list of the collections for each specific user. You can then still use security rules to ensure the user can only access the collections their ID is mentioned in.

Also see:

  • Firestore Comparison Operators - contains, does not contain, starts with
  • Firestore - How to apply 'string contains' condition on a field while querying?

Firestore: How to query Documents where DocumentID begins with a substring?

How to query Documents where DocumentID begins with a substring?

There is currently no way you can achieve that.

I have not found anything online that tells how to do this on Document ID fields.

You haven't found anything because such a query does not exist.

My research tells me it's possible for field values by using '<=' in where()

Yes, that's the only option you have now. So add all those Geohashes which are now documents ids as values of a new (geohash) property within your document.

I also found this as a potential solution:

docs = mycolRef.where(firebase.firestore.FieldPath.documentId(), '>', somestring).stream();

Unfortunately, you cannot do that, hence that error.

One more thing to note is that in Firestore, it is not possible to change the document ids once it exists. All that data is immutable. What you can do instead is to read all the documents, change the document id and then put it all back.

Firestore query a document based on starts with in any of the 2 fields

You won't be able to do this in one query, because Firestore does not support logical OR queries that span multiple fields. What you can do instead is make two queries, one for name, and the other for username that do string prefix sorts (read other questions about that here, here, here). Then, after you do both queries, merge the results on the client to build the final list of results.

firestore query document that have a collection contains a document

Firestore queries can only order/filter on fields in the documents that they return. There is no way to filter on fields in other documents, or the existence of other documents.

The common way to deal with this is to have a field in the parent document that indicates whether the child document you're looking for exists, and updated that on every relevant write. Then you can use that field to filter the documents in collection A.



Related Topics



Leave a reply



Submit