AWhitford
AWhitford

Reputation: 3998

Cloud Firestore subcollection access

When accessing subcollections, should one use code like this:

DocumentSnapshot userSnapshot = await Firestore.instance
          .collection('users')
          .document(userId)
          .collection('shoppingLists')
          .document(listName)
          .get();

or this:

DocumentSnapshot userSnapshot = await Firestore.instance
          .collection('users/$userId/shoppingLists')
          .document(listName)
          .get();

?

I prefer the first style. Do they translate to the same I/O?

Upvotes: 2

Views: 95

Answers (2)

sllopis
sllopis

Reputation: 2368

From the Documentation:

Every document or collection in Cloud Firestore is uniquely identified by its location within the database, and you can create a reference that points to it. For convenience, to access the Cloud Firestore hierarchical data model, you can create references by specifying the path to a document or collection as a string, with path components separated by a forward slash (/).

Therefore, you can choose to use either of the two methods you posted earlier or shorten it even further as @AndreyGordeev suggested. It's really up to you.

Upvotes: 1

Andrey Gordeev
Andrey Gordeev

Reputation: 32479

There's no difference in result.

Actually, you can even shorten the second one to:

DocumentSnapshot userSnapshot = await Firestore.instance
          .document('users/$userId/shoppingLists/$listName')
          .get();

Upvotes: 1

Related Questions