简体   繁体   English

检查 Firestore 中的文档是否存在

[英]Check for document existence in Firestore

I'm trying to find if some item exists in my database.我正在尝试查找我的数据库中是否存在某些项目。

If it does not exist, I had like to add it.如果它不存在,我想添加它。

If it exists, I had like to show a message.如果它存在,我想显示一条消息。

The code im using:我使用的代码:

CollectionReference colRefMyBooks = db.collection( "Users" ).document( auth.getUid() ).collection( "MyBooks" );
Query queryMyBooks = colRefMyBooks.whereEqualTo("BookID", bookId);
queryMyBooks.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Toast.makeText(BookDetailActivity.this, "Book already in my list", Toast.LENGTH_SHORT).show();
            }
        } else {
            db.collection( "Users" ).document( auth.getUid() ).collection( "MyBooks" ).add( general_book );
        }
    }
});

This code works good as long as there is a collection "MyBooks" .只要有一个集合"MyBooks" ,此代码就可以正常工作。 However, if there is no collection "Mybooks" I want it to consider it as the task is not successful and therefore to add the item.但是,如果没有集合"Mybooks" ,我希望它认为任务不成功,因此添加该项目。

What I do get is that it skips the whole onComplete and therefore does not add anything.我得到的是它跳过了整个 onComplete ,因此没有添加任何东西。

Does it mean that I have to check first if a collection exists and inside of it if document?这是否意味着我必须先检查一个集合是否存在,如果有文档则在其中?

Thank you谢谢

A query that finds no documents will always be considered "successful", regardless of whether or not any collections exist.无论是否存在任何 collections,一个未找到文档的查询将始终被视为“成功”。 This behavior can't be changed.此行为无法更改。

What you'll have to do instead is check if there are no documents in the result set and decide what you want to do from there:相反,您需要做的是检查结果集中是否没有文档,然后从那里决定您要做什么:

queryMyBooks.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            QuerySnapshot snapshot = task.getResult();
            if (snapshot.isEmpty()) {
                db.collection( "Users" ).document( auth.getUid() ).collection( "MyBooks" ).add( general_book );
            }
            else {
                for (QueryDocumentSnapshot document : snapshot) {
                    Toast.makeText(BookDetailActivity.this, "Book already in my list", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
});

Note that collections don't really "exist".请注意,collections 并不真正“存在”。 There are no operations to add or delete collections.没有添加或删除 collections 的操作。 There are just operations to modify documents.只有修改文档的操作。 A collection only appears in the console if it contains at least one document.集合仅在包含至少一个文档时才会出现在控制台中。 If it contains no documents, it just disappears.如果它不包含任何文档,它就会消失。

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

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