Reputation: 61
I am trying to create a new document without any data (any fields) in it. But I can't find a way.
Upvotes: 3
Views: 2264
Reputation: 1676
According to new document you can try like this:
val mData = hashMapOf<String, String>()
db.collection("collectionName").document("documentName").set(mData)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.toString()}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding document", e)
}
Upvotes: 0
Reputation: 41
You can use set() and provide an empty object.
firebase.firestore().collection('myCollection').doc('docId').set({})
Upvotes: 4
Reputation: 138824
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:
Upvotes: 5