简体   繁体   English

我可以记录 Firestore 文档的更改吗?

[英]Can i record changes on firestore documents?

I'm working on a project with Firebase and Firestore, I have a collection that has a status attribute and I wanna record the time that a document is updated and who updated it.我正在使用 Firebase 和 Firestore 开发一个项目,我有一个具有状态属性的集合,我想记录文档更新的时间以及更新者。 There's no documentation for this Does anyone have a solution?没有这方面的文档 有人有解决方案吗?

There is nothing built-in that can help you achieve that.没有任何内置功能可以帮助您实现这一目标。 If you want to track the changes that are made to a document and know who made them, you have to create a mechanism for that yourself.如果您想跟踪对文档所做的更改并知道是谁做的,您必须自己创建一个机制。

Assuming that you have a document with a particular ID, you can create a subcollection in which you can add as documents the changes that are made.假设您有一个具有特定 ID 的文档,您可以创建一个子集合,您可以在其中将所做的更改作为文档添加。 A possible database schema would look like this:可能的数据库模式如下所示:

Firestore-root
   |
   --- users (collection)
        |
        --- $uid (document)
             |
             --- changes (collection)
                  |
                  --- $changeId (document)
                        |
                        --- timestamp: Febrary 4, 2022 at 2:46:19 PM UTC+3
                        |
                        --- madeBy: "UidOfTheUser"

In this way you can keep a history of the changes that are made to a particular document.通过这种方式,您可以保留对特定文档所做更改的历史记录。

Something like this happened to me when i wanted to retrive live messages from Firebase so i used Stream Builder to catch all new messages in the stream.当我想从 Firebase 检索实时消息时,我遇到了这样的事情,所以我使用 Stream Builder 来捕获 stream 中的所有新消息。

Here is the code i used!这是我使用的代码!

class MessagesStream extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _firestore.collection('messages').orderBy('time').limitToLast(100).snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }
        final messages = snapshot.data.docs.reversed;
        List<MessageBubble> messageBubbles = [];
        for (var message in messages) {
          final messageText = message.data()['text'];
          final messageSender = message.data()['sender'];
          final currentUser = loggedInUser.email;
          final messageBubble = MessageBubble(
            sender: messageSender,
            text: messageText,
            isMe: currentUser == messageSender,
          );
          messageBubbles.add(messageBubble);
        }
        return Expanded(
          child: ListView(
            reverse: true,
            padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
            children: messageBubbles,
          ),
        );
      },
    );
  }
}

Instead of storing this information yourself, you can also use Firestore's built-in audit logging capability to track who access what documents.您也可以使用Firestore 的内置审核日志记录功能来跟踪谁访问了哪些文档,而不是自己存储这些信息。 Check the documentation to learn how to enable audit logging for data access .查看文档以了解如何为数据访问启用审核日志记录

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

相关问题 我无法从 FireStore 打印多个文档 - I can't print multiple documents from FireStore 如何计算 swift 中 firestore 查询的文档数? - How can I count the documents from a firestore query in swift? 我可以听取 Firestore 文档中的单个值更改吗 - Can I listen to individual value changes in a Firestore Document 如何检查 Cloud Firestore 中任何文档的集合中是否存在值(例如名称)? - How can I check if a value e.g. name exists in a collection within any of documents in Cloud Firestore? Firestore - 我可以像收集列表一样获取所有文档 ID 吗? - Firestore - Can I get all documents ID just like collection list? 如何通过子数组中项目的 ID 获取 firestore 文档 - How can I fetch firestore documents by the Id of an item in it's sub array 根据 Firestore 文件新增或删除的变化重新加载 tableView - Reload tableView based on the changes of Firestore documents added or deleted 我可以使用 Firestore 检索具有相同名称的不同集合中相同子集合的文档吗? - Can I retrieve documents of same subcollection in different collections with the same name, using Firestore? 如何插入具有唯一 GUID 的新 Firestore 集合文档? - How can I insert new Firestore collection documents with unique GUID for an id? 在 firestore 中加载文档时出错,如何在 Z Scaler 中解决此问题 - Error loading documents in firestore, how can I resolve this issue in Z Scaler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM