Getting Started with Firebase 9 #8 - Ordering Data & Timestamps

Net Ninja · Beginner ·🌐 Frontend Engineering ·4y ago

Key Takeaways

The video demonstrates how to order data and use timestamps in Firebase 9, specifically using the orderBy function and serverTimestamp to create and order documents by creation time.

Full Transcript

all right then gang so when we retrieve documents from a collection whether that be through the get docs function or the on snapshot function the documents that we get back seem to be in no apparent order they're not in alphabetical order when it comes to the author or the title or any other kind of property but in actual fact they are they're in order according to the id now remember firebase automatically generates these ids for us and they are random so by ordering by id then they are in kind of a random order okay according to our data but we can order our queries so that we can order them by properties so i could say okay order them by author value so they're alphabetical by order or title or some other kind of property and that's what we're going to do now okay so to do this is really simple all we need to do is first of all import a function called order by from firestorm then down here when we create the query we need to pass it in as another argument so after the where clause over here i'm going to pass in the order by function now the order by function takes in an argument which is the property we want to order by so for example i could say order by title and then we can pass in descending or ascending right here so i could say descending for example as the second argument now if i save this and preview in the browser then we're going to see this error in the console and it's basically saying look we can't just get this query without first creating an index for it so click on this link and that's going to open up your firestore console to create an index so just click on this button to do that right here and this will just take a couple of minutes to do and while the status is building it means it's still building and this won't work so we just have to wait for a few minutes all right then so once you see this status as enabled it means that we can go ahead and get that query where we're ordering by the title in descending order so if we take a look over here and refresh now if we open up this array then we should see that it's ordered by the title so you can see the wise man sphere the name of the wind and then abc so the letters nearer to zed are starting first and then the letters nearer to a are at the end because this is in descending order if it was in ascending order then the first one would be this then this and then this all right so we've ordered by the title but now what i'd like to do is create another property on our object right here or on our documents called createdat and that would be a timestamp and it will represent the time that a user added that bulk and then we can order by that timestamp so the first thing i'm going to do is delete all of these documents right here now to do that i'm just going to go to our data in the fire store and by the way i could have done it from here if i wanted to but this the way is quicker because i'm going to go over here and delete the collection and if i type in the collection right here it's going to delete all the documents inside that collection for us so that now in the future whenever we add new documents we're going to add that timestamp value to it as well all right then so first of all we want to add a created app property to every new document that we create so that we can order by that created at property so i could say created at right here and we need to set that equal to be a timestamp now we don't just use a date object or anything like that when we're using firestore we use a special firestore timestamp and we need to import a function to create one of those so up here i'm going to import something called server timestamp like so now when we invoke that function it creates a timestamp for us which is stored in the firestore documents so down here i'm just going to say server timestamp and invoke that to create a timestamp to be the value of this created that property so now every new book we create is going to have that timestamp which says when the book was created and then i want to order by that created that property so i'm going to go over here and get rid of this and then i'm just going to say by created at like so now by default it's in ascending order so i'm going to leave it right here we don't need to pass in the argument ascending and then also if we wanted to we don't have to have the where clause right here if i just want to get every document then i can delete this it doesn't really matter which is what i'm going to do and i'm going to save this now we're going to add a few documents and try this out all right then so we can see that the data is empty currently so let's add some new documents i'm going to say the wise man's fear first of all which is by patrick rothfuss and then add that book i'm also going to add in a new one by him which is the name of the wind and then patrick rothfuss all right enter and then i'm going to add in another one the way of kings that is by brandon sanderson and press enter so now they should be ordered by that created at property so let me just open up this most recent snapshot right here and we can only see one inside there so what i'm gonna do is actually just refresh to make sure we're getting the correct code okay so now we see three and now inside here we can see that we have a created up property in each of these individual objects and they're now ordered by that created that property we can see this order right here is the order that we added them we added this one first then this and then this one and if we add in a new one let me add in the final empire by brandon sanderson and press enter that one should now also appear at the bottom so you'll see brandon sanderson down here at the bottom the final empire and the others are still in the correct order as well now you'll have noticed when i'm adding a new book now i'm actually getting the data logged to the console twice let me just show you a b c d e f at the book and we can see the data logged twice so why is that well it's because we added on that timestamp so when we do that it sends the information to firestore it adds the document and we get a snapshot back but then it takes a short amount of time for firestore then to add the timestamp and when it adds that to the document it sends us back a new firestore snapshot because the data has changed does that make sense so that's why we're getting these two different things load to the console right here every time we add a new document

Original Description

🐱‍👤 Course files: https://github.com/iamshaunjp/Getting-Started-with-Firebase-9 🐱‍👤 My Premium Udemy courses: https://thenetninja.co.uk/udemy/react-and-firebase https://thenetninja.co.uk/udemy/vue-and-firebase https://thenetninja.co.uk/udemy/modern-javascript https://thenetninja.co.uk/udemy/d3-and-firebase 🐱‍👤 Get access to all other premium courses on Net Ninja Pro: https://netninja.dev/ 🐱‍💻 My Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to order data in Firebase 9 using the orderBy function and how to use timestamps to track document creation time. It covers the basics of data querying and indexing in Firestore.

Key Takeaways
  1. Import the orderBy function from Firebase
  2. Create a query with the orderBy function
  3. Pass the property to order by as an argument
  4. Create an index for the query in the Firestore console
  5. Use serverTimestamp to create a timestamp for each document
  6. Order documents by the createdAt property
💡 When using serverTimestamp, Firestore sends a new snapshot when the timestamp is added to the document, which can result in the data being logged to the console twice.

Related Reads

Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →