5

I am wondering if Cloud Firestore timestamps each document on creation in its metadata, and if so, is that metadata available?

I know I can create my own timestamps inside a document for arbitrary reasons, but I'm only interested in when the document itself was created.

Thanks in advance

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
thunk
  • 434
  • 3
  • 10
  • Note that if you really don't want to add a Timestamp field when you create the doc from your front end, as explained by Alex, you could use a [Cloud Function](https://firebase.google.com/docs/functions/firestore-events) to do so (`onCreate` event). – Renaud Tarnec Jan 17 '20 at 12:53
  • Thanks Renaud. Its not that I'm adverse to trigger functions. Its just that I'm trying to establish the exact time the document is written to establish ordering. Putting it in a cloud function would not provide me with this (i.e. with a trigger function it would be after the fact and possible retries mean that it could be way inaccurate) – thunk Jan 20 '20 at 10:22
  • I do understand your requirement! I was just mentioning another possibility :-) – Renaud Tarnec Jan 20 '20 at 11:09
  • Ahh, no worries Renaud. Thanks. – thunk Jan 20 '20 at 12:17

2 Answers2

15

The accepted answer is actually not true for the latest beta release of firestore, you can access the creation date from the meta data e.g. in python like this:

doc.create_time

in JavaScript you can get the date like this:

const creationDate = new Date(doc.createTime._seconds * 1000)

Just have a look at the documentation: https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1

Daniel Cukier
  • 11,502
  • 15
  • 68
  • 123
danem
  • 159
  • 1
  • 5
  • 1
    This should be the accepted answer. Firebase Firestore provides `create_time` and `update_time` timestamps in v1 if you only need output. https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#document – axelra82 Sep 18 '21 at 11:35
  • This would be great to use but I can't seem to get a query working in Angular Firestore. – Tawm Oct 03 '21 at 04:36
  • 1
    @Tawm - In the Node version, it's doc.createTime – Tim Jan 26 '22 at 20:29
  • @Tawm I don't believe you can query by metadata...you can only access it. – Cooper Scott Jun 07 '22 at 17:59
1

Edit Apr 27'th 2023:

In google.firestore.v1 the document contains now create_time and update_time, both being of type Timestamp.


Edit Jan 21'th 2022:

In Android, you can call DocumentSnapshot.getMetadata() method or QuerySnapshot.getMetadata(), which both return an object of type SnapshotMetadata. By the time I'm editing this answer, in this class there is no method that returns a timestamp. There is only a hasPendingWrites() and isFromCache().


I am wondering if Firestore timestamps each document on creation in its metadata

No, it does not.

I know I can create my own timestamps inside a document

That's the solution you should go ahead with. Add the timestamp of the creation as a field within every document.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks Alex. Do you knw if this server timestamp() generated by client or the server(database)? – thunk Jan 17 '20 at 13:23
  • 1
    The server timestamp is generated on Firebase servers. Only the documents ids are generated on the client. – Alex Mamo Jan 17 '20 at 13:26