How to get partial string query matches in Firebase Cloud Firestore? [duplicate] - firebase

This question already has answers here:
Google Firestore: Query on substring of a property value (text search)
(25 answers)
Closed 3 years ago.
When I search for 'stone', I want my query to return
{ first: 'Fred', last: 'Flintstone', }
However, the docs suggest I need to search for an exact match only.
usersRef.where("last", "==", "Flintstone")
And will not return a partial match of 'stone'.
Is there a way to get partial string search matches?

This is not supported by firebase as the link you shared mentioned, probably you want to use some tool like algolia to achieve that use case, and keep it in sync with firestore using cloud functions.

Related

Firebase getting a list of documents [duplicate]

This question already has answers here:
Google Firestore - How to get several documents by multiple ids in one round-trip?
(17 answers)
Closed 8 months ago.
In Firestore, if I have as list of, say, 20 document IDs, what is the best way to fetch all those documents?
I'm thinking of, for example, using something like Angolia for search and getting a list of ids back from it.
Would I need to manually get or onSnaptshot each reference, or is there a more performant/simple way to fetch a list of documents just using their ID?
Thanks!
A lot of the options depends on how your firestore is structured, and what framework you're using to encorporate firebase.
Generally speaking, querying a list can be done using the in query operator.
In the case of id querying, the array is id's, and you query by firebase.firestore.FieldPath.documentId()
import { query, where } from "firebase/firestore";
const q = query(firebase.firestore.FieldPath.documentId(), where('id', 'in', ['SOME_ID_1', 'SOME_ID_2']));

how to change one element value of specific index of array in firebase uisng flutter? [duplicate]

This question already has answers here:
Is there any way to update a specific index from the array in Firestore
(3 answers)
Closed 2 years ago.
i want to change taken value to true on specific index of notification time stamp (array in fire base ,shown in image)so how i write my query
[1]: https://i.stack.imgur.com/aGWpI.jpg
my code:-
await FirebaseFirestore.instance
.collection('Prescriptions')
.doc(medicineDetails['id'].toString())
.update({
'NotificationTimeStamp ': FieldValue.arrayUnion([{}])
});
you can only add or remove from an array, but as a work around you can do this by changing the array in code the replacing the array in firebase.

Retrive all collection except one document id Firebase [duplicate]

This question already has answers here:
Firestore: how to perform a query with inequality / not equals
(7 answers)
Closed 2 years ago.
I want to retrieve all the collections except one document id. I have following query it will get all data by validating collection field. however i want to get all data except one document.
await Firestore.instance.collection('tbl').where('id',isEqualTo:textId).getDocuments().then(
(data){
dataRowCount= data.documents.length;
}
);
Firestore doesn't offer any way to exclude documents from a query. You must can only filter for known values that you're looking for, or ranges of values. If you want to skip a document, you will have to check the query results in your code and omit the one you don't want

Firebase Collection Group Query on Id / Key [duplicate]

This question already has an answer here:
How to perform collection group query using document ID in Cloud Firestore
(1 answer)
Closed 2 years ago.
I have been following along the following document:
https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query
My data structure roughly looks like this:
/teams/{teamid}
{
displayName: "Company X Team",
owner: "userid",
}
/teams/{teamid}/invites/{emailAddressAsKey}
{
someProp: "my data"
}
In my web app I want to search through all the different teams records to find an invite who's id/key is equal to the email address that I pass in. After reading through the documentation, I think a Collection Group Query is what I'm looking for. However, my situation doesn't exactly match the example. I want to match on the key, not a prop within the document. I suppose I could add the email address again as a prop, but that doesn't feel right.
You have to put the document ID as a key in the document itself. There is currently no other solution.
It's tempting to use FieldPath.documentId() in a where clause (eg following Alex Mamo's answer https://stackoverflow.com/a/56967352/2518722), but that doesn't work in the general case. The reason is that FieldPath.documentId() actually refers to the full, unique ID of the document including its path.
If you do try this you'll get the following error:
Error: When querying a collection group and ordering by
FieldPath.documentId(), the corresponding value must result in a valid
document path, but '<your doc id>' is not because it contains an odd
number of segments.
Basically the where clause only works with searches on keys/values not document IDs.
You can solve this with the help of FieldPath.documentId() like in the following line of code:
db.collectionGroup('teams').where(firebase.firestore.FieldPath.documentId(), '==', 'teams/teamId').get()
This query will return all documents with a specific team id.

How to get a names List of collections indexed in a collection, in firebase in Flutter? [duplicate]

This question already has answers here:
Firebase (2016) Shallow Query
(2 answers)
Closed 3 years ago.
I have the next datas on real time firebase database:
example
As you see, there are more data in "animals" , "rocks" and "vegetables". I want get that names in a List of data, something like this:
List list = ["animals","rocks", "vegetables"];
I know how to do a get http request in flutter to get data doing this:
var response = await http.get('https://mydb.firebaseio.com/prueba.json');
pruebaFirebase = json.decode(response.body);
But it returns a List of collections, and I only need the name of each one names of collections.
Thanks in advance dear community.
Use the shallow query parameter of the Realtime Database REST API:
https://mydb.firebaseio.com/prueba.json?shallow=true

Resources