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

前端 未结 5 1536
鱼传尺愫
鱼传尺愫 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:10

    This is telling you that territories/6 does not exist as an actual document, whereas territories/7 does.

    In Cloud Firestore it is possible to have subcollections owned by "virtual" documents - that is the document at the higher level doesn't exist, but it has children.

    These virtual documents can be easy ways to organize information without have to create duplicate dummy documents.

    In this case, you've either:

    1. Created a bunch of dispatch documents under territories/6 before you created the territories document, or
    2. You've subsequently deleted territories/6 without deleting the subcollection documents.
    0 讨论(0)
  • 2021-01-12 01:18

    A simple fix

    A document needs to have at least one field for it to exist. If a document only contains sub-collections, then it is said to be a virtual/non-existent document and for this reason it is not possible for it to appear in queries or data snapshots.

    consider adding a field like this

      // initialize firestoreDatabase
    
        val firestoreDatabase : FirebaseFirestore? by lazy { FirebaseFirestore.getInstance() }
    
        // set at least one field
        
        firestoreDatabase.collection("territories").
                                      document("6").
                                      set(hashMapOf(
                                  "key" to "value"))
    

    do a similar thing to any other document in your database.

    0 讨论(0)
  • 2021-01-12 01:23

    I was able to fix this bug, the accepted answer currently only explains the why and does not provide a fix. Here is the code that caused the bug (for me) when I attempted to run this without a "users" collection at the root of my Firestore:

    db.collection("users").document(currentUserUID).collection("groups").addDocument(data: [
            "groupID": newGroup.documentID,
            "userAddDate": NSDate()
        ])
    

    The currentUserUID document that was created was throwing the 'Document does not exist' virtual bug. I copied the currentUserUID and deleted the users collection and virtual UserUID document.

    I then created a new 'users' collection at the root node and pasted in the value I had copied for the userUID with a bogus field.

    Then when I re-ran the above snippet of code, the "groups" collection and document were re-added no problem :)

    0 讨论(0)
  • 2021-01-12 01:24

    Not sure this will resolve your problem but did mine... the message in console: "This document does not exist, it will not appear in queries or snapshots" seems to appear if the document has only subcollections but no fields (I guess this is referred to as "virtual" document).

    I used set method to add dummy fields (json objects) on already existing "virtual" documents which made them discoverable by snapshots or queries. It did not overwrite/delete any subcollections of those documents. Fields could not be added via console.

    My code:

    var docRef = this.fsdb.collection('sessions')
    //do for all "virtual" docs in collection
    docRef.doc('mySession1').set({dummy: 'dummy'})
    
    docRef.onSnapshot(val => {
       var mySessionsArray: any [] = []
       val.forEach(myDoc => {
        mySessionsArray.push(myDoc.id)
      })
      console.log(mySessionsArray)
    })
    

    console: ["mySession1", "mySession2", "mySession3"]

    Disclaimer, not a programmer by trade :-)

    0 讨论(0)
  • 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<String, String>())
    

    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.

    0 讨论(0)
提交回复
热议问题