Does firestore auto generated id depend on path? [duplicate] - firebase

This question already has answers here:
Is it possible to receive duplicate IDs with Firebase .doc()?
(1 answer)
Firestore: Are ids unique in the collection or globally?
(2 answers)
Closed 2 days ago.
Could a unique ID be a duplicate if I generate an ID from a different path? like:
const id1 = firestore.collection("chat").doc().id;
const id2 = firestore.collection("profile").doc().id;
Is there any chance id1 and id2 could duplicate?

Is there any chance id1 and id2 could duplicate?
The collision of IDs in the case of Firestore is incredibly unlikely and you can/should consider they'll be completely unique. That's what they were designed for. So you don't have to worry about it.
Keep in mind that, the built-in generator for unique IDs which is used in Firestore when you call the CollectionReference#add() method or CollectionReference#document() method without passing any arguments, generates random and highly unpredictable ids, which prevents hitting certain hotspots in the backend infrastructure.
If you're interested in how exactly the document IDs are generated, please check #FrankvanPuffelen's answer from the following post:
Does the path of a Document have any bearing on the random ID auto-generated by Firestore?
Does a unique ID could duplicate if I generate an ID from a different path?
No. Each time you call .doc().id, a new random document ID is generated.

Related

Firebase firestore unique values in multiple fields

I'm creating an application for generating documents with unique id, my issue is that the id needs to be in a specific format ( 00000/A/B) so I can't use firestore document's id's.
The problem is also that I have that id's in different places,
#1 case
users/{userID}/id = //UNIQUE ID HERE
#2 case
users/{userID}/members <= members is an array of objects where every member need a unique id
I was thinking about the separate collection of id's where I can check which one is taken but maybe is there a better way to ensure id is unique across the whole app?
What you're considering is pretty much the only way to guarantee uniqueness of a value across the database.
In a few more structured steps, it'd be:
Use that value as the document ID in an secondary collection. This collection purely exists to ensure uniqueness of the IDs.
Let the user claim it, typically by writing their UID into the document.
Use security rules to ensure a user can only write a document if it doesn't exist yet, and (if needed) only deleted when they own it.
The topic of unique values has been covered quite a few times before, although usually in the form of unique user names, so I recommend checking out:
Cloud Firestore: Enforcing Unique User Names
How to generate and guarantee unique values in firestore collection?
How to enforce Uniqueness in a Property of a document field in Google Cloud Firestore
Firestore unique index or unique constraint?
I want to make unique usernames in firebase/firestore

How does String id = db.collection("myCollection").document().getId() gives document Id without hitting Firestore database?

I read somewhere that db.collection("mycollection").document().getId(); gives document Id in mycollection without hitting cloudstore database. But how it is possible to create unique Id without knowing document id of already existing doucments or hitting couldstore?
The auto-ID that is generated when you call document() is a fairly basic UUID (universally unique identifier). Such identifier are statistically guaranteed to be unique. In my words: there is so much random information in there, that the chances of two calls generating the same value are infinitesimally small.
So Firestore doesn't actually call the server to check whether the ID it generates is unique. It instead relies on the mathematical properties of picking a single value out of a sufficiently large and random set to be very certain it is unique.

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

Is it possible to query collectionGroup on a specific collection? [duplicate]

This question already has answers here:
collectionGroup within a certain path
(2 answers)
Closed 3 years ago.
I have a user collection, and within that collection there's a collection name groups, which then has groupItems. Is it possible to run a query for all groupItems for a specific user?
I could run a global version like:
db.collectionGroup('groupItems')
but that is overkill for me, I'm looking for something like:
db.collection('users').doc(uid).collection('groups').collectionGroup('groupIems').where('categoryOnly', '==', true)
Is this possible?
The architecture of the collections looks like this:
Users
---> User
------> Collections
---------> GroupA
------------> GroupAItems
---------------> GroupAItem1
---------------> GroupAItem2
---------> GroupB
------------> GroupBItems
---------------> GroupBItem1
---------------> GroupBItem2
Ideally I could call for all Group Items instead of first calling for groups and getting [GroupA, GroupB...], and then calling for GroupA's items, then GroupB's, etc.
Update: it turns out that querying a specific path may be possible after all, thanks to how FieldPath.documentId() is indexed for collection group indexes. Check #samthecodingman's answer here: https://stackoverflow.com/a/68049847
Happy answer above 👆
Old answer below 👇
There is currently no way to limit a collection group to just collections under a specific path. All collection group queries get documents from all collections with the specified name.
So the only way to currently query a subset of your groups is to use a naming scheme that allows you to uniquely address them.
Also see:
Does collection group queries get data from all collections with the same name?
collectionGroup within a certain path

Firestore generated unique ids for more then 1 collection

Within the project that I am currently working on a document need to be placed from one collection to the other one.
This is done by deleting a document from one collection and save it into another collection (using the same ID).
The problem I am facing is regarding the uniqueness of the generated ID.
Question1:
Can Firestore generate an ID that once already occurred in that collection even tho the document having that ID was removed?
Imagine the following collection:
collectionY[
1: {},
2: {}
...
]
We remove document with id 1 and save it in another collection. Will collectionY ever generate id 1 again for any new documents?
Will collectionY ever generate id 1 again for any new documents?
The collisions of ids in this case is incredibly unlikely and you can/should assume they'll be completely unique. That's what they were designed for. So you don't have to be concerned about it.
This built-in generator for unique ids that is used in Firestore when you call CollectionReference's add() methods or CollectionReference's document() method without passing any parameters, generates random and highly unpredictable ids, which prevents hitting certain hotspots in the backend infrastructure.

Resources