Query Firestore to get documents in a range of documentIDs [duplicate] - firebase

This question already has answers here:
Google Firestore - How to get several documents by multiple ids in one round-trip?
(17 answers)
Closed 4 years ago.
I'm developing a Flutter app using Firebase and Cloud Firestore.
In Firestore I have a collections of Users.
I want to perform a query who give me a list of Users with documentId in a range.
Example
My database:
> Users --> docId1 --> {name: "Domenico"}
> --> docId2 --> {name: "Antonio"}
> --> docId3 --> {name: "Maria"}
My desidered query is something like:
Select name from Users
where documentId in (docId2, docId3)
who gives me ["Antonio", "Maria"].
On documentation I've found how to filter by a document key but not a collection by a range of ducumentId.
Can you help me to perform this query or to understand if I'm doing something wrong?
Thank you.

You can't query on a range of id.
You have to do Two distinct query
var query = firestore.collection("users").doc(docId);
But if you just want to filter on user Name you can do this
var query = firestore.collection("users").where('name', isEqualTo: 'Domenico');
This kind of query is one of the disadvantage of using firestore.

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']));

Firestore collection group query on document id [duplicate]

This question already has an answer here:
How to perform collection group query using document ID in Cloud Firestore
(1 answer)
Closed 1 year ago.
I am trying to run the following query:
this.db.firestore
.collectionGroup('members')
.orderBy('dateModified', 'desc')
.where(firebase.firestore.FieldPath.documentId(), '==', uid)
But I get the error:
Invalid query. When querying a collection group by
FieldPath.documentId(), the value provided must result in a valid
document path, but 'X6yL5Ko55jSMWhhoQUO91xVYdEH3' is not because it
has an odd number of segments (1).
Is the only way around this to add the document id to the document?
This is not ideal as I have a lot of existing data...
The document id is the uid of the firebase user.
As the error message says, for an index on a collection group the documentId() field values are actually stored as document paths to ensure unique lookups of those values in the index.
If you want to also query on document ID over a collection group, you will indeed have to store the ID as a field value in each document.
Also keep in mind that it is then possible to get multiple documents for the query, even though that is astronomically unlikely if you use the built-in add() operation.
Adding the uid to the document itself is the only possible way at the moment and then query on that field:
this.db.firestore
.collectionGroup('members')
.orderBy('dateModified', 'desc')
.where("uid", '==', uid)
There was a Github issue for the same and explains why that's not possible.
That's pretty much why I sometimes prefer to store a root level collection members. Each document in the collection will have contain the groupID (or whatever your parent collection is meant for). If you use userID as the key for documents in there then it goes easy.
this.db.firestore.collection("members").doc(uid)
So instead of having a path like: /groups/{groupID}/members/{memberID}, the structure will be like: /groups/{groupID} and all the members will be store in the root level collection 'members'. A document in that collection may look like:
// uid as doc key
{
groupId: "groupID",
...otherFields
}
The catch is if a member can join multiple groups you cannot use the userId as the key.

FireStore - How to not allow creation of document if there is another document with the same value in a field [duplicate]

This question already has answers here:
Firestore unique index or unique constraint?
(5 answers)
Enforcing unique userIds in Firebase Firestore
(1 answer)
firebase rule for unique property in firestore
(2 answers)
Closed 2 years ago.
I have a Firestore document collection called registrations. Inside registrations, there are documents that contain name, phone number, and age of persons. I want to write the security rules database which allows the creation of person document if and only if another person with the same name does not exist on the database
Thanks in advance
It sounds like you want to keep your names distinct, such as with a UNIQUE constraint in SQL.
It is not possible to query specific document fields in Firestore security rules.
There are two ways to do what you want with Firestore:
use name as your document id
or create a collection /usedNames with name as document id, so that in your security rule you can test:
let name = request.resource.data.name;
return exists(/usedNames/$(name)) == false

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

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