简体   繁体   中英

How to save document id in the document field in android Firestore?

I am able to save the document to the firestore, but I also want to save the document id of the newly saved as well to the same document, I am trying the below example but not good

 String id = db.collection("user_details").document().getId();

                                Map map = new HashMap<>();
                               map.put("username", username);
                                map.put("email", email);
                                map.put("id", id);




                                UserRef.document(id).set(map).addOnSuccessListener(new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void aVoid) {

                                        //progressbar invisible;
;

                                    }
                                });

Every time you call document() , you're going to get a new unique ID. So, be sure to call it only once, so you only deal with one ID.

First get the DocumentReference:

DocumentReference ref = db.collection("user_details").document();

Get its ID:

String id = ref.getId();

Then compose the data to send:

Map map = new HashMap<>();
map.put("username", username);
map.put("email", email);
map.put("id", id);

And finally, put that data in the document referenced earlier:

ref.set(map)...

To be able to save your ID in a document first you need to creat one. Problem is that ID is created at the same time as a document is created. But we can first create ID and than send our document like this:

val matchRef = mFirestore.collection(FirebaseHelp().USERS).document(user.uid).collection(FirebaseHelp().MATCHES).document() //notice this will not create document it will just create reference so we can get our new id from it
val newMatchId = matchRef.id //this is new uniqe id from firebase like "8tmitl09F9rL87ej27Ay" 

The document is not yet created we just have a new id so now we add this id to our POJO class (or it is POKO I guess because it is Kotlin).

class MatchInfo(
        var player1Name: String? = null,
        var player2Name: String? = null,
        var player3Name: String? = null,
        var player4Name: String? = null,
        var firebaseId: String? = null, //this is new added string for our New ID
)

So now we create our object we want to upload to firebase:

val matchInfo = MatchInfo(player1?.mName, player2?.mName, player3?.mName, player4?.mName, newMatchId)

or we set our new id just before sending the object to firebase

matchInfo.firebaseId = newMatchId

and now we send our object to firebase with our new ID like this:

val matches = mFirestore.collection(FirebaseHelp().USERS).document(user.uid).collection(FirebaseHelp().MATCHES)
matches.document(newMatchId).set(matchInfo) // this will create new document with name like"8tmitl09F9rL87ej27Ay" and that document will have field "firebaseID" with value "8tmitl09F9rL87ej27Ay"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM