This document does not exist and will not appear in queries or snapshots, but identically structured document works

前端 未结 5 1613
鱼传尺愫
鱼传尺愫 2021-01-12 00:45

Preface: this question is already asked here but user gave up and gave solution to first answer. This question also differs in that I have two similar collection structures,

5条回答
  •  余生分开走
    2021-01-12 01:28

    Here is how I understand it. In Firebase Cloud Firestore, you can create a document without creating its parent document. In other words, it's perfectly OK to add "mysubdoc" without creating "mydoc" beforehand.

    [Kotlin]
    FirebaseFirestore.getInstance()
        .document("/mycoll/mydoc/mysubcoll/mysubdoc").set(hashMapOf("key", "value"))
    

    If you do, there is no harm, except that you get "This document does not exist, it will not appear in queries or snapshots" warning and possibly some queries might not work as expected.

    Conversely, deleting a document does not delete its subcollections. Refer to https://firebase.google.com/docs/firestore/manage-data/delete-data for more info.

    If you do want to create the parent document, you can do something similar to the following; create a document with an empty value.

    FirebaseFirestore.getInstance()
        .document("/mycoll/mydoc").set(hashMapOf())
    

    Make sure the read/write rules are correctly set, especially if you use the user's ID as a part of the document path to prevent unauthorized users from snooping around.

    If it's just a one-time deal, you can create "mydoc" from Firebase console as well.

提交回复
热议问题