I am trying to create a new document without any data (any fields) in it. But I can't find a way.
2 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:

- 130,605
- 17
- 163
- 193
You can use set() and provide an empty object.
firebase.firestore().collection('myCollection').doc('docId').set({})

- 31
- 1
- 1
-
1Thanks ! Worked with dart as well. I think this should be the accepted answer :) – Jack' Mar 09 '22 at 00:00