简体   繁体   English

如何从 FireStore 查询文档名称?

[英]How can I query for a document name from FireStore?

Look I have created a Google Sign In method and after that a setup that should show up only for the first time when signin in in that screen he will enter some information about himself that will be uploaded into a document to firestore.看,我已经创建了一个谷歌登录方法,然后一个设置应该只在第一次登录时显示在该屏幕上,他将输入一些关于他自己的信息,这些信息将被上传到一个文档中到 firestore。 That firestore document will have the same UID as the google sign in user.该 Firestore 文档将具有与 google 登录用户相同的 UID。 In order to figure it out if the user already has an account with some information provided I want to do a query for if a document already exists with that specific uid as their documentname.为了弄清楚用户是否已经拥有提供某些信息的帐户,我想查询是否已经存在以该特定 uid 作为其文档名的文档。

FirebaseUser user = mAuth.getCurrentUser();
FirebaseFirestore db;

String currentID = mAuth.getCurrentUser().getUid();

// Checken ob User schon Dokument hat
db = FirebaseFirestore.getInstance();
db.collection("users")
         .whereEqualTo("uid", currentID)
         .get()
         .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { // Add the listener callback

@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
    if(task.isSuccessful()) {
        // Check the size of the result to see if any matches were found
        if(task.getResult().size() == 0) {
            // No document exists containing the searched value
        } else {
            // A document already exists containing the searched value
            hasDocument = true;
        }
    } else {
        Log.e(TAG, "There was an error querying the documents.", task.getException());
    }
}
});

if(hasDocument) {
     SendUserToMainActivity();
} else {
     SendUserToSetupActivity();
     Toast.makeText(LoginActivity.this, "You must provide some information first", Toast.LENGTH_LONG).show();
 }

I think my problem is that I am querying for the field and I don´t know how to query for the documentname instead of the field itself.我认为我的问题是我正在查询该字段,但我不知道如何查询文档名而不是字段本身。 How can I do that?我怎样才能做到这一点?

这是它的样子

This is the way the documents are structured:这是文档的结构方式:

Use the following code to check whether user already have document or no使用以下代码检查用户是否已有文档

FirebaseUser user = mAuth.getCurrentUser();
FirebaseFirestore db;
String currentID = mAuth.getCurrentUser().getUid();
//Checken ob User schon Dokument hat
db = FirebaseFirestore.getInstance();
db.collection("users")
        .document(currentID)
        .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());
                }
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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