3

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

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Wimansha
  • 61
  • 1
  • 7
  • Why would you want to do that? Is there a use case where saving non-existant data would apply? – Jay Aug 15 '19 at 13:23

2 Answers2

5

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:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
3

You can use set() and provide an empty object.

firebase.firestore().collection('myCollection').doc('docId').set({})
Chaim Fishman
  • 31
  • 1
  • 1