Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to model Custom Object for Document ID in Firestore?

Say I have a collection of 'Users', and am happy for their ID to be the generated Firestore documentId, something like:

Users Collection
    GENERATED_FIRESTORE_ID1:
        name: "User 1 name"
        ...: etc.
    GENERATED_FIRESTORE_ID2:
        name: "User 2 name"
         ...: etc."

and I am adding them, and retrieving them with a custom object (I'm using Android at the moment but the question I guess is more generalistic). I don't want to have an extra "id" field in the document, just use the document.getId() method to get the generated firestore ID.

Is there a correct way to map a POJO to not have an indivual ID field, but when querying set it for application usage? I am doing it using the @Exclude annotation as follows:

public class User {

// as a side question, do I need @exclude on the field or just the getter?
@Exclude
String uId;

String name;
String email;
//... additional fields as normal

public User() {
}

@Exclude
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.displayName = name;
}

//... etc. etc.

}

and then I create the User object and set its ID as follows:

 for (DocumentSnapshot doc : documentSnapshots) {
     User user = doc.toObject(User.class);
     user.setId(doc.getId());
     users.add(user );
 }

This works fine, and apologies if this is indeed the way, but I'm new to FireStore (am loving it) and want to make sure I'm doing it right. I just wondered if there was a way this would all be automatic, without @Exclude and then manually setting the ID after doc.toObject(MyCustomObject.class)

like image 252
Vin Norman Avatar asked Feb 05 '18 11:02

Vin Norman


People also ask

How do I add document ID to firestore?

Set the data of a document within a collection, explicitly specifying a document identifier. Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier. Create an empty document with an automatically generated identifier, and assign data to it later.

Is firestore document ID unique?

The keys generated by calling add() in Firestore are not tied to the collection on which you call add() . Instead they are random identifiers that are statistically guaranteed to be unique.

Can two documents have the same ID in firestore?

New document with same ID should not be allowed in the same collection. It should be possible to fetch an already existing document from a previous import.

How do I get a random document ID in firestore?

An easy way to grab random documents is get all the posts keys into an array ( docA , docB , docC , docD ) then shuffle the array and grab the first three entries, so then the shuffle might return something like docB , docD , docA .


1 Answers

There is now an Annotation for this -

You could simply use

@DocumentId String uID

https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentId.html

like image 136
Jeff Padgett Avatar answered Oct 26 '22 07:10

Jeff Padgett