question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Specify Id of document in RXFirestore

See original GitHub issue

Is it possible to specify the id of the documents we create via RXFirestore.addDocument()? I find this very useful feature missing. Could someone help show how to do it?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
juliendercommented, Jun 12, 2018

Thanks ! But not. But I found ! After hours of struggling. I forked the repo and found in javadoc setDocument that

if the document does not yet exist, it will be created.

Wonderful. Here is the result.

public static Maybe<User> getUser(){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    return RxFirebaseAuth.signInAnonymously(FirebaseAuth.getInstance())
            .flatMap(authResult -> {

                DocumentReference userRef = db.collection("users").document(authResult.getUser().getUid());
                User user = new User(authResult.getUser().getUid());

                return RxFirestore.getDocument(userRef, User.class)    // get user
                        .switchIfEmpty(RxFirestore.setDocument(userRef, user).toMaybe());   // update user if does not exist
            });
}

I can now create users documents named with the authentication user ID, so no need to use queries, everything is very simple.

Thanks again for your help

1reaction
FrangSierracommented, May 8, 2018

AddDocument recieve a Document or CollectionReference. You just need to create the ID before and add it to your new reference before call AddDocument

val newFollowedUserId = firestore.collection(PUBLIC_PROFILE).document(uid).collection(FOLLOWING).document()

RxFirestore.addDocument(firestore.collection(PUBLIC_PROFILE).document(uid).collection(FOLLOWING).document(newFollowedUserId)).subscribe()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add Document with Custom ID to firestore
To use a custom ID you need to use .set , rather than .add. This creates a document with the ID "LA":
Read more >
Add data to Cloud Firestore - Firebase - Google
There are several ways to write data to Cloud Firestore: Set the data of a document within a collection, explicitly specifying a document...
Read more >
Add a Firestore document using an autogenerated id
// Add a new document with a generated id. puts "Added document with ID: #{added_doc_ref.
Read more >
Firebase V9 Firestore Add Document Data – addDoc()
1. Initialize Firestore Database · 2. Collection() Method · 3. Document Data {} · 4. Add Document Data Using addDoc() · 5 Get...
Read more >
JavaScript : How to add Document with Custom ID to firestore
JavaScript : How to add Document with Custom ID to firestore [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] JavaScript : How ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found