Checking if a document exists in a Firestore collection










0















I have a Firestore collection tree in which I plan to store only one document. I want to check if that collection contains that document and if so, retrieve the id of the document! Any ideas/thoughts?



Thanks :)










share|improve this question




























    0















    I have a Firestore collection tree in which I plan to store only one document. I want to check if that collection contains that document and if so, retrieve the id of the document! Any ideas/thoughts?



    Thanks :)










    share|improve this question


























      0












      0








      0








      I have a Firestore collection tree in which I plan to store only one document. I want to check if that collection contains that document and if so, retrieve the id of the document! Any ideas/thoughts?



      Thanks :)










      share|improve this question
















      I have a Firestore collection tree in which I plan to store only one document. I want to check if that collection contains that document and if so, retrieve the id of the document! Any ideas/thoughts?



      Thanks :)







      java android firebase google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 10:20









      Alex Mamo

      46.6k82965




      46.6k82965










      asked Nov 16 '18 at 6:20









      Emil SharierEmil Sharier

      165




      165






















          3 Answers
          3






          active

          oldest

          votes


















          1














          Try this one.



          FirebaseFirestore firestore = FirebaseFirestore.getInstance();
          firestore.collection("users").whereEqualTo("user_uid", user.getUid())
          .limit(1).get()
          .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
          @Override
          public void onComplete(@NonNull Task<QuerySnapshot> task)
          if (task.isSuccessful())
          boolean isEmpty = task.getResult().isEmpty();


          );





          share|improve this answer






























            1














            Make the document structure like this one:



            collectionName(Collection) -> documentId(Document) -> id = documentId(and other fields)



            Now make a query:



            FirebaseFirestore firestoreDatabase= FirebaseFirestore.getInstance();
            firestoreDatabase.collection(COLLECTION_NAME)
            .whereEqualTo(DOCUMENT_ID, documentId)
            .get()
            .addOnCompleteListener(task ->
            if (task.isSuccessful())
            if (task.getResult().getDocuments().size() > 0)
            // Here is your document with id

            );





            share|improve this answer






























              1














              If you want to check a particular document for existence based on its document id, please use the following lines of code:



              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
              DocumentReference docIdRef = rootRef.collection("yourCollection").document(docId);
              docIdRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>()
              @Override
              public void onComplete(@NonNull Task<DocumentSnapshot> task)
              if (task.isSuccessful())
              DocumentSnapshot document = task.getResult();
              if (document.exists())
              Log.d(TAG, "Document exists!");
              else
              Log.d(TAG, "Document does not exist!");

              else
              Log.d(TAG, "Failed with: ", task.getException());


              );


              If you don't have the document id, you need to use a query and find that document according to a value of a specific property like this:



              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
              CollectionReference yourCollRef = rootRef.collection("yourCollection");
              Query query = yourCollRef.whereEqualTo("yourPropery", "yourValue");
              query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
              @Override
              public void onComplete(@NonNull Task<QuerySnapshot> task)
              if (task.isSuccessful())
              for (QueryDocumentSnapshot document : task.getResult())
              Log.d(TAG, document.getId() + " => " + document.getData());

              else
              Log.d(TAG, "Error getting documents: ", task.getException());


              );





              share|improve this answer

























              • Hi Emil! Have you tried my solutions above, does it work?

                – Alex Mamo
                Nov 17 '18 at 8:57










              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332471%2fchecking-if-a-document-exists-in-a-firestore-collection%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Try this one.



              FirebaseFirestore firestore = FirebaseFirestore.getInstance();
              firestore.collection("users").whereEqualTo("user_uid", user.getUid())
              .limit(1).get()
              .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
              @Override
              public void onComplete(@NonNull Task<QuerySnapshot> task)
              if (task.isSuccessful())
              boolean isEmpty = task.getResult().isEmpty();


              );





              share|improve this answer



























                1














                Try this one.



                FirebaseFirestore firestore = FirebaseFirestore.getInstance();
                firestore.collection("users").whereEqualTo("user_uid", user.getUid())
                .limit(1).get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task)
                if (task.isSuccessful())
                boolean isEmpty = task.getResult().isEmpty();


                );





                share|improve this answer

























                  1












                  1








                  1







                  Try this one.



                  FirebaseFirestore firestore = FirebaseFirestore.getInstance();
                  firestore.collection("users").whereEqualTo("user_uid", user.getUid())
                  .limit(1).get()
                  .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                  @Override
                  public void onComplete(@NonNull Task<QuerySnapshot> task)
                  if (task.isSuccessful())
                  boolean isEmpty = task.getResult().isEmpty();


                  );





                  share|improve this answer













                  Try this one.



                  FirebaseFirestore firestore = FirebaseFirestore.getInstance();
                  firestore.collection("users").whereEqualTo("user_uid", user.getUid())
                  .limit(1).get()
                  .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                  @Override
                  public void onComplete(@NonNull Task<QuerySnapshot> task)
                  if (task.isSuccessful())
                  boolean isEmpty = task.getResult().isEmpty();


                  );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 6:31









                  HussainAbbasHussainAbbas

                  799




                  799























                      1














                      Make the document structure like this one:



                      collectionName(Collection) -> documentId(Document) -> id = documentId(and other fields)



                      Now make a query:



                      FirebaseFirestore firestoreDatabase= FirebaseFirestore.getInstance();
                      firestoreDatabase.collection(COLLECTION_NAME)
                      .whereEqualTo(DOCUMENT_ID, documentId)
                      .get()
                      .addOnCompleteListener(task ->
                      if (task.isSuccessful())
                      if (task.getResult().getDocuments().size() > 0)
                      // Here is your document with id

                      );





                      share|improve this answer



























                        1














                        Make the document structure like this one:



                        collectionName(Collection) -> documentId(Document) -> id = documentId(and other fields)



                        Now make a query:



                        FirebaseFirestore firestoreDatabase= FirebaseFirestore.getInstance();
                        firestoreDatabase.collection(COLLECTION_NAME)
                        .whereEqualTo(DOCUMENT_ID, documentId)
                        .get()
                        .addOnCompleteListener(task ->
                        if (task.isSuccessful())
                        if (task.getResult().getDocuments().size() > 0)
                        // Here is your document with id

                        );





                        share|improve this answer

























                          1












                          1








                          1







                          Make the document structure like this one:



                          collectionName(Collection) -> documentId(Document) -> id = documentId(and other fields)



                          Now make a query:



                          FirebaseFirestore firestoreDatabase= FirebaseFirestore.getInstance();
                          firestoreDatabase.collection(COLLECTION_NAME)
                          .whereEqualTo(DOCUMENT_ID, documentId)
                          .get()
                          .addOnCompleteListener(task ->
                          if (task.isSuccessful())
                          if (task.getResult().getDocuments().size() > 0)
                          // Here is your document with id

                          );





                          share|improve this answer













                          Make the document structure like this one:



                          collectionName(Collection) -> documentId(Document) -> id = documentId(and other fields)



                          Now make a query:



                          FirebaseFirestore firestoreDatabase= FirebaseFirestore.getInstance();
                          firestoreDatabase.collection(COLLECTION_NAME)
                          .whereEqualTo(DOCUMENT_ID, documentId)
                          .get()
                          .addOnCompleteListener(task ->
                          if (task.isSuccessful())
                          if (task.getResult().getDocuments().size() > 0)
                          // Here is your document with id

                          );






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 6:34









                          Rehman Murad AliRehman Murad Ali

                          274




                          274





















                              1














                              If you want to check a particular document for existence based on its document id, please use the following lines of code:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              DocumentReference docIdRef = rootRef.collection("yourCollection").document(docId);
                              docIdRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<DocumentSnapshot> task)
                              if (task.isSuccessful())
                              DocumentSnapshot document = task.getResult();
                              if (document.exists())
                              Log.d(TAG, "Document exists!");
                              else
                              Log.d(TAG, "Document does not exist!");

                              else
                              Log.d(TAG, "Failed with: ", task.getException());


                              );


                              If you don't have the document id, you need to use a query and find that document according to a value of a specific property like this:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              CollectionReference yourCollRef = rootRef.collection("yourCollection");
                              Query query = yourCollRef.whereEqualTo("yourPropery", "yourValue");
                              query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<QuerySnapshot> task)
                              if (task.isSuccessful())
                              for (QueryDocumentSnapshot document : task.getResult())
                              Log.d(TAG, document.getId() + " => " + document.getData());

                              else
                              Log.d(TAG, "Error getting documents: ", task.getException());


                              );





                              share|improve this answer

























                              • Hi Emil! Have you tried my solutions above, does it work?

                                – Alex Mamo
                                Nov 17 '18 at 8:57















                              1














                              If you want to check a particular document for existence based on its document id, please use the following lines of code:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              DocumentReference docIdRef = rootRef.collection("yourCollection").document(docId);
                              docIdRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<DocumentSnapshot> task)
                              if (task.isSuccessful())
                              DocumentSnapshot document = task.getResult();
                              if (document.exists())
                              Log.d(TAG, "Document exists!");
                              else
                              Log.d(TAG, "Document does not exist!");

                              else
                              Log.d(TAG, "Failed with: ", task.getException());


                              );


                              If you don't have the document id, you need to use a query and find that document according to a value of a specific property like this:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              CollectionReference yourCollRef = rootRef.collection("yourCollection");
                              Query query = yourCollRef.whereEqualTo("yourPropery", "yourValue");
                              query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<QuerySnapshot> task)
                              if (task.isSuccessful())
                              for (QueryDocumentSnapshot document : task.getResult())
                              Log.d(TAG, document.getId() + " => " + document.getData());

                              else
                              Log.d(TAG, "Error getting documents: ", task.getException());


                              );





                              share|improve this answer

























                              • Hi Emil! Have you tried my solutions above, does it work?

                                – Alex Mamo
                                Nov 17 '18 at 8:57













                              1












                              1








                              1







                              If you want to check a particular document for existence based on its document id, please use the following lines of code:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              DocumentReference docIdRef = rootRef.collection("yourCollection").document(docId);
                              docIdRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<DocumentSnapshot> task)
                              if (task.isSuccessful())
                              DocumentSnapshot document = task.getResult();
                              if (document.exists())
                              Log.d(TAG, "Document exists!");
                              else
                              Log.d(TAG, "Document does not exist!");

                              else
                              Log.d(TAG, "Failed with: ", task.getException());


                              );


                              If you don't have the document id, you need to use a query and find that document according to a value of a specific property like this:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              CollectionReference yourCollRef = rootRef.collection("yourCollection");
                              Query query = yourCollRef.whereEqualTo("yourPropery", "yourValue");
                              query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<QuerySnapshot> task)
                              if (task.isSuccessful())
                              for (QueryDocumentSnapshot document : task.getResult())
                              Log.d(TAG, document.getId() + " => " + document.getData());

                              else
                              Log.d(TAG, "Error getting documents: ", task.getException());


                              );





                              share|improve this answer















                              If you want to check a particular document for existence based on its document id, please use the following lines of code:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              DocumentReference docIdRef = rootRef.collection("yourCollection").document(docId);
                              docIdRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<DocumentSnapshot> task)
                              if (task.isSuccessful())
                              DocumentSnapshot document = task.getResult();
                              if (document.exists())
                              Log.d(TAG, "Document exists!");
                              else
                              Log.d(TAG, "Document does not exist!");

                              else
                              Log.d(TAG, "Failed with: ", task.getException());


                              );


                              If you don't have the document id, you need to use a query and find that document according to a value of a specific property like this:



                              FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
                              CollectionReference yourCollRef = rootRef.collection("yourCollection");
                              Query query = yourCollRef.whereEqualTo("yourPropery", "yourValue");
                              query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
                              @Override
                              public void onComplete(@NonNull Task<QuerySnapshot> task)
                              if (task.isSuccessful())
                              for (QueryDocumentSnapshot document : task.getResult())
                              Log.d(TAG, document.getId() + " => " + document.getData());

                              else
                              Log.d(TAG, "Error getting documents: ", task.getException());


                              );






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 16 '18 at 12:42

























                              answered Nov 16 '18 at 10:16









                              Alex MamoAlex Mamo

                              46.6k82965




                              46.6k82965












                              • Hi Emil! Have you tried my solutions above, does it work?

                                – Alex Mamo
                                Nov 17 '18 at 8:57

















                              • Hi Emil! Have you tried my solutions above, does it work?

                                – Alex Mamo
                                Nov 17 '18 at 8:57
















                              Hi Emil! Have you tried my solutions above, does it work?

                              – Alex Mamo
                              Nov 17 '18 at 8:57





                              Hi Emil! Have you tried my solutions above, does it work?

                              – Alex Mamo
                              Nov 17 '18 at 8:57

















                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332471%2fchecking-if-a-document-exists-in-a-firestore-collection%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線