Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new Firestore document without data?

I am trying to create a new document without any data (any fields) in it. But I can't find a way.

like image 672
Wimansha Avatar asked Mar 14 '26 14:03

Wimansha


1 Answers

I am trying to create a new document without any data (any fields) in it. But I can't find a way.

There is no way you can create a document without any data. You can use the following lines of code:

let db = Firestore.firestore()
let docRef = db.collection("yourCollection").document("yourDocument")
let documentID = docRef.documentID

But this should give you only the id of the document. Please note that it doesn't mean that it will create a document for you. With other words, it merely "reserves" an id for a document in a particular collection. So for a document to actualy exist, you should add at least a property of any type. It can even hold a null value. In fact, why to have a document if it doesn't hold anything?

However, if you want to create a subcollection within a document, you can use a document that actually does not exist. It will be displayed in italic, as explained in my answer from the following post:

  • Firestore document/subcollection is not existing
like image 121
Alex Mamo Avatar answered Mar 17 '26 04:03

Alex Mamo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!