What's the difference between using a DocumentReference in Firestore and using just the id?

后端 未结 1 1907
一整个雨季
一整个雨季 2020-12-17 23:25

In Firestore you can create objects with data type Reference. But this is just the path to said document. What\'s the difference between using this and just using the id as

相关标签:
1条回答
  • 2020-12-17 23:42

    A Reference contains the entire path to a document, whereas a simple string ID has no context. Granted, you could just store the path as a string instead, but for convenience (and ease of use in custom objects) it can be useful to have the entire Reference object stored.

    The sort order of a Reference is also different to that of a String. From the Supported Data Types documentation:

    • Reference sort order: By path elements (collection, document ID, collection, document ID...)
    • Text string sort order: UTF-8 encoded byte order

    This means that you can also filter on a Reference object in the database by comparing it to another when writing queries.

    For example:

    var reference = db.collection("test").document("1");
    var query = db.collection("test").orderBy("ref").where("ref", ">", reference);
    
    0 讨论(0)
提交回复
热议问题