How do I get the document ID for a Firestore docum

2019-01-23 08:09发布

问题:

I have kotlin data class

data class Client(

    val name: String = "",
    val email: String = "",
    val phone: String ="") {
constructor():this("","","")}

I have firestore populate the data into the class just fine, however I am at a loss trying to figure out how to get the document id into the data class without having to set it in the document itself. Is this possible?

回答1:

Yes, it's possible to get id without storing it, using DocumentSnapshot. I will try to build complete examples here.

I created a general Model class to hold the id:

@IgnoreExtraProperties
public class Model {
    @Exclude
    public String id;

    public <T extends Model> T withId(@NonNull final String id) {
        this.id = id;
        return (T) this;
    }
}

Then you extend it with any model, no need to implement anything:

public class Client extends Model

If I have list of clients here, trying to query the list to get only clients with age == 20:

clients.whereEqualTo("age", 20)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) {
                        // here you can get the id. 
                        Client client = document.toObject(client.class).withId(document.getId());
                       // you can apply your actions...
                    }
                } else {

                }
            }
        });

And if you are using EventListener, you can also get the id like the following:

clients.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
        for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
            // here you can get the id. 
                        Client client = document.toObject(client.class).withId(document.getId());
                       // you can apply your actions...
        }
    }
});

documentSnapshot.getId()) will get you the id of the Document in the collection without saving the id into the document.

Using Model will not let you edit any of your models, and don't forget using @IgnoreExtraProperties



回答2:

I solved it by creating a extension method on QueryDocumentSnapshot like this:

inline fun <reified T : HasId>QueryDocumentSnapshot.toObjectWithId(): T {
    val model = this.toObject(T::class.java)
    model.id = this.id
    return  model
}

Now I can map like this (which I find to look nice and clean):
myModelWithId = it.toObjectWithId<MyModel>()

For this to work I made a simple hasId interface that the model needs to implement:

interface HasId{
    var id : String
}

@IgnoreExtraProperties
data class MyModel(
        @get:Exclude override var id : String    = "",
        val data                     : String    = "",

): Serializable, HasId


回答3:

Here is how I solved the problem.

data class Client(
    val name: String = "",
    val email: String = "",
    val phone: String ="",
    @get:Exclude var id: String = "") {
constructor():this("","","")
}

I use @get:Exclude on the id to make sure the id doesn't get sent to Firestore on save, and then when fetching the list of clients do:

snapshot.documents.mapTo(list) {
            var obj = it.toObject(Client::class.java)
            obj.id = it.id
            obj
        }

Setting the id of the new object to the id of the document reference.



回答4:

I just improved the solution of @iCediCe, and add methods to DocumentSnapshot and QuerySnapshot.

interface HasId {
    var id : String
}

inline fun <reified T : HasId> DocumentSnapshot.toObjectWithId(): T {
    return this.toObject(T::class.java)!!.also {
        it.id = this.id
    }
}

inline fun <reified T : HasId> QuerySnapshot.toObjectsWithId(): List<T> {
    return this.documents.map {
        it.toObjectWithId<T>()
    }
}

And usage:

data class User(
    @get:Exclude
    override var id: String,
    ...
): HasId


val user = documentSnapshot.toObjectWithId<User>()
val users = querySnapshot.toObjectsWithId<User>()