简体   繁体   English

Firestore:批量更新插入文档

[英]Firestore: Bulk Upsert documents

I am new to firestore as well as NoSQL. I want to make a simple database which is somewhat relational, with two tables, In terms of relational databases, one is operational (patients) and one is a transactional (readings) table.我是 firestore 以及 NoSQL 的新手。我想制作一个有点关系的简单数据库,有两个表,就关系数据库而言,一个是操作(患者)表,一个是事务(读数)表。

I have an app that generates data for a patients table and a readings table.我有一个为患者表和读数表生成数据的应用程序。 One app instance may contain data of many unique patients and each patients may have different readings in the readings table.一个应用程序实例可能包含许多独特患者的数据,每个患者在读数表中可能有不同的读数。

In Firestore I plan to achieve this by making two collections, for each of the tables mentioned and bulk upload documents for each patient and reading in the corresponding collection.在 Firestore 中,我计划通过为提到的每个表格制作两个 collections 并为每个患者批量上传文档并阅读相应的集合来实现这一点。 Now every time I do a bulk upload to the firestore, I want to check if all the fields in the input patient JSONObjects exist in the patient collection than pick up the patientID else, make a new patient, and generate an ID and enter the reading with the given patientID.现在每次我批量上传到 firestore 时,我想检查输入患者 JSONObjects 中的所有字段是否都存在于患者集合中,而不是选择 patientID else,创建一个新患者,并生成一个 ID 并输入读数使用给定的 patientID。

I tried to do this with transactions but that only works for a single document, as mentioned here, ( Can we not query collections inside transactions? ) With Batch only set(), update() and delete() operations are possible as mention in the documentation, with batchget too we can only search using the document ID and not the fields.我尝试对事务执行此操作,但这仅适用于单个文档,如此处所述,( 我们可以不在事务中查询 collections 吗? )使用 Batch 仅可以进行 set()、update() 和 delete() 操作,如中所述文档,对于 batchget,我们也只能使用文档 ID 而不是字段进行搜索。

Is there any way I can do this, are there any issues with the schema?有什么办法可以做到这一点,模式有什么问题吗?

Indeed, trying to batch read data from your collection and then, updating it, won't be possible directly.事实上,尝试从您的集合中批量读取数据然后更新它是不可能直接实现的。 As clarified in this other similar case here , you can read up to 10 documents at once, using IN to fetch multiple documents, however it's not very feasible.正如此处其他类似案例中所阐明的那样,您最多可以一次读取 10 个文档,使用IN获取多个文档,但这不是很可行。

The best for you to do it, without changing your scheme, would be to read the collection entirely with a forEach() and then, perform a batch update from specific documents, based in the id that you return.在不更改方案的情况下,最好的做法是使用forEach()完全读取集合,然后根据返回的 id 从特定文档执行批量更新。 This way, for example, with the patientId, you should be able to update all the readings.这样,例如,使用 patientId,您应该能够更新所有读数。 The below code show to perform the batch, once you have the id.下面的代码显示执行批处理,一旦你有 id。

DocumentReference patRef = db.collection("patients").document("id");
batch.update(patRef, "Value1", "Value2"); //Update two sample of fields in the document

batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        // ...
    }
});

The other option would be to work with Subcollections , so you can have all your data stored in one father collections and it subcollections.另一种选择是使用Subcollections ,因此您可以将所有数据存储在一个父亲 collections 及其子集合中。 This way, you can deal with everything within one query, which for sure, would make your work easier.这样一来,您就可以在一个查询中处理所有事情,这肯定会让您的工作更加轻松。

I would say that the second one is better, however, involves restructuring your scheme.我会说第二个更好,但是涉及重组您的计划。 The good part is that once you have done it, your work will be much easier.好的部分是,一旦完成,您的工作就会容易得多。

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

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