-
-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Firestore Add Sub-Collection Example #153
Comments
I don't think is such a basic request, it's definitely something worth looking at not easy to add in a declarative way. this.$bind('collection', collectionRef, { onDocumentBind })
function onDocumentBind(doc, bind) {
// bind is bound to doc
bind('comments', db.collection(`collection/${doc.id}/comments`), { onDocumentBind: () => {} })
} this would allow me to automatically unbind the collection when document is unbound. But I still need to check if it's feasible 🙂 |
Just saw this, maybe you havent see it yet and it'll help. Thanks 👍 https://cloud.google.com/nodejs/docs/reference/firestore/0.8.x/DocumentReference#getCollections |
Unfortunately, that only exists on the node.js library 😞 |
@posva I think that even the example you gave using the constructed string path ( |
I'm glad it helped! I will add it to the docs once the whole thing is available, that will be easier to understand and to contextualise in docs |
Howdy! I've just recently found this framework, and I couldn't find the aforementioned example for subcollection fetching in the docs. I'm building a music player application with the following structure: Edit: If this isn't quite implemented yet, I guess I can move the songs to their own top-level collection and instead add an array of reference objects to the user documents . |
@posva is there any update on this? or should I just have two bound firestore ref, 1 for the main collection (e.g. |
@SumNeuron yes, you still need to do that |
can you please provide a clean example? I am struggling |
Firestore doc's encourage users (in some cases) to nest their data in sub-collections. I think that it is likely a common practice so any documentation here on how to manage that would be helpful. Thank you for the great tool! |
@mfissehaye, @wonbyte, @cloudwheels - What additional update are you hoping for? Let's try to give Posva something specific to consider! Here's my understanding: TodaySub-collections are separate collections. Every Firestore query is limited to a single collection1. The nested relationship doesn't change this. If you want a doc AND its children, that's 2 queries. Today (AFAIK) VueFire has no special case for this. ✅ Ex1a: Single doc + children - just bind twice (you can do this now): bind('ticket', db.collection(TICKETS).doc(doc.id))
bind('comments', db.collection(TICKETS).doc(doc.id).collection(COMMENTS)) ✅ Ex2a: Many docs + "children" - I think your best bet is to avoid sub-collections: bind('tickets', db.collection(TICKETS))
bind('comments', db.collection(COMMENTS)) // put ticketId into your comment docs
...
// group comments by ticket: { ticketId1: [comments], ticketId2: [comments], ... }
get keyedComments() { return groupBy(this.comments, 'ticketId') } // getter + lodash Potential Feature Ideas (NOT AVAILABLE TODAY)❌ Ex1b: Single doc + children (as one object/binding) - access sub-collection as nested property: bind('ticket', db.collection(TICKETS).doc(doc.id), { with: COMMENTS })
// ticket.comments would contain the subcollection data IMO, not worth it, because you still need to save the comment data separately, and I don't want to accidentally push my subcollection data into my doc. ❌ Ex2b: Many docs + children (as subcollections) - optimize binding to get children of all "tickets" in one query: bind('tickets', db.collection(TICKETS))
bindChildren('commentsByTicket', db.collectionGroup(COMMENTS)) // collectionGroup query spans all tickets
// commentsByTicket would be keyed: { ticketId1: [comments], ticketId2: [comments], ... } I believe this is impossible today because Firestore Collection group queries don't include any information about which parent they're nested under, so it's impossible for VueFire to group them. So we'd still need to use a ticketId property (already possible as Ex2a). Suggested ActionsTo focus the discussion, I suggest:
1 1 collection or collection group, but the latter probably doesn't help in the parent+child case. |
@charles-allen How does your above example work for related comments when a user replies to a comment? Do you nest replied comments under the parent comment? How deep do you nest for additional responses to that replied comment? HOpe this makes sense... BTW, over at fireship.io they have an example video tutorial on doing nested comments in firestore and they used AngularFire. Any chance someone can translate that to Vuefire? :) |
@dosstx - I think this is a database design problem, not a VueFire one. AFAIK, VueFire mirrors the standard Firebase SDK. If you need multiple queries through the SDK, you need multiple binds with VueFire. If your db design is optimized for the standard SDK, it's equally optimized for VueFire (which binds to the exact same query references). Your database design needs to be driven by your business, UX, and security requirements. How to design your database is probably a better question for https://stackoverflow.com or maybe https://dba.stackexchange.com. I'd probably start by looking at 2 angles (disclaimer: I'm not a NoSQL expert!!):
|
Well, I've been at it a week or too, and I'm just too green to figure out how to reference a sub-collection from within a template (if that's even possible), or what the best way to get this data would be.
What Im attempting to accomplish is loop through a collection of
tickets
, in which each ticket has a sub-collection ofcomments
. I'd want to display all comments, for each ticket in the list.Heirarchy goes: DB > Tickets Collection > ticketdocs > Comments Collection > commentdocs
app.js:
list.html.js:
Displaying/iterating over
{{ ticket }}
works great and binds up fine, but accessing the sub-collection is where I'm stumped.I'm sorry for such a basic request with poor descriptions/examples, but with the new Firestore sub-collections, Im sure some folks would love some examples of how to get access to sub-collections in the various ways available.
Thank you!
The text was updated successfully, but these errors were encountered: