Im looking to have a document, which contains a collection of references to other documents. Like a Project that has a collection of Users, but the entries are Reference types to users from the top level Users collection.
I can't see a way of adding a Reference type to a Collection, It looks like you must create a Document first and then have a field in that doc that is the reference to the user doc - but that is just adding extra work.
When getting the collection data, you will have to first get the Document in the collection, and then using the field for the reference get the User Document.
It would be far simpler to have a nested array of values.
What is the best way to store a collection of references ?
A Reference is a field type, and fields must be in documents. You indeed cannot add References directly to a collection. But from your description I don't think this requires extra dummy documents:
Im looking to have a document, which contains a collection of references to other documents. Like a Project that has a collection of Users, but the entries are Reference types to users from the top level Users collection.
This sounds like you have two top-levels collections: Projects and Users. A document under Projects then has a nested map of user references.
If I misunderstood your model, then indeed: you will need to create a document to store your references to users. This is inherent to the Firestore data model, and can't be changed.
Related
User0 has a has runbooks stored in
/Runbook/user0/runbooks/0...n
UserX has runbooks stored at
/Runbook/userX/runbooks/0...n
UserX would like to include user0/runbooks/55 in his runbooks collection but doesn't want to move a copy into the runbooks collection. Is there a way to add a document into UserX's runbooks collection that will point to user0's 55 document?
You can simply store enough information about the document in a field (for example, its ID or its full path). Or you can store a reference type field that also essentially just contains the full path of the document.
See also:
Documentation
What is Firebase Firestore 'Reference' data type good for?
How to get the referenced data from a Firebase Firestore reference?
There is no type of document that itself is just a reference to another document. References are only valid as a document field type. You will have to model your data to account for this fact.
Is there a way to add a document into UserX's runbooks collection that
will point to user0's 55 document?
No. There is not. Duplicate data is common in document databases like Firestore, or Real Time Database. The correct thing to do is to duplicate the data.
How does one filter a Firestore collection by a document reference in the web console? For example, I have a collection that references another collection document called challenges.
But no matter what combination I use to try to filter that collection by the challenge ID it returns no results. I need to be able to do this so I can look at all of the documents in this collection that reference back to that particular challenge reference.
I am assuming this is possible in the console, but maybe it is not?
im trying to make a sort of chat app with the following architecture:
Its currently working as follows: in a root doc, there is an array of chat objects. Each chat object spawns a message doc, which contains an array of message objects. Same logic for comments.
Im thinking about a function to update all posts relative to the associated user, IE if a user changes their name all associated comments will be updated. Is this possible with the WhereIn() function? Or should i edit the architecture to something more like each document being its own message/comment? Thanks!
An in clause checks if a specific field is equal to one of a set of values, which doesn't apply here.
You might be thinking of array-contains, but that wouldn't work in your current structure either. The array-contains only matches exact an item in the array if it completely/exactly matches the value in the query.
The common way to allow such a query is to add a participants array field to each document where you store the UIDs of all participants in that doc. Then you can do an array-contains query against that.
I'm using Firebase Cloud Firestore and can't figure out how to access documents without knowing the specific path. The database structure is users/{user id}/favourites/{favourite id}. There are no fields in users/{user id} only subcollections. Knowing the user id, i can get the favourites for the user, but I can't get a list of users to get everyone's favourites. Here is the code I am trying (Java admin SDK):
db.collection("users").get().get()
which results in an empty Iterable with no DocumentSnapshots.
How can I get a list of the most popular favourites?
EDIT: I've discovered I can get a list of users with no fields if I add a field. Even if I delete it later, it still appears as a document in the collection.
EDIT2: I've discovered that I can create an empty document, so I'm just doing that for now. As a one-off, I can get a list of all users from firebase auth and look up which ones have a favourites collection and just set those to empty documents.
Firebase Firestore has a reference type while defining fields of a document which allows us to add a reference to another document via its "Document path".
For example, I have the document animals/3OYc0QTbGOTRkhXeiW0t, with a field name having value Zebra. I reference it in the array animals, of document zoo/xmo5wX0MLUEbfFJHvKq6. I am basically storing a list of animals in a zoo, by referring the animals to the corresponding animal document in the animals collections.
Now if I query a specific document from the zoo collection, will references to the animals be automatically resolved? Will I the get the animal names in the query result? If not, how can I achieve this?
All document queries in Firestore are shallow, meaning that you only get one document in return for each document requested.
References in a document are not automatically fetched - you will have to make subsequent queries using the references in the document to get those other documents on your own.
Same thing with documents in subcollections - they require separate queries.