英文:
Add new key value pair to firebase map
问题
当前情况
我有以下的云 Firestore 数据库:
我正在使用 Python 进行工作,目前正在使用以下代码进行更新:
city_ref.update({"test": {"three": 200}})
我知道数据字段的确切路径以及要添加数据的映射。
我想要实现的目标
我想要能够将数据添加并更新为以下内容。
我希望仅通过一次写入来实现:即不需要从数据库中读取数据,将数据附加到已读取的数据,并更新数据库。
问题
我的当前代码会更新整个映射如下所示。
我如何在保留现有数据的同时添加数据?
英文:
Current situation
I have the following cloud firestore DB:
I'm working on Python and currently using the following code to update it:
city_ref.update({"test": {"three": 200}})
I know the exact path to the data field and map to add data.
What I would like to achieve
I want to be able to add data and update it to following.
I would like to archive just by one write: meaning without the process of reading from database, appending data to read data, and updating database.
Problem
My current code code updates the whole map as below.
How could I add data while leaving the existing data ?
答案1
得分: 0
要向文档写入内容,您需要知道该文档的准确完整路径并构建一个引用它的引用(在您的代码中称为 city_ref
)。如果您知道路径并且有引用,您可以在不首先读取文档的情况下写入内容。
但是,如果您不知道路径的一部分(最常见的是要更新的文档的ID),那么您将需要从集合中读取文档以便能够更新它们。
根据您提供的更新:您当前的指令告诉数据库使用您指定的值来更新顶层的 test
字段。但是您想要做的是写入嵌套字段,这需要使用 .
符号表示:
city_ref.update({"test.three": 200})
英文:
In order to write to a document, you need to know the exact, complete path to that document and construct a reference to it (the city_ref
in your code). If you know the path and have a reference, you can write to the document without first reading it.
But if you don't know a part of the path (most commonly the ID of the document you want to update), then you will have to read the document(s) from the collection in order to be able to update them.
Based on the update you provided: your current instruction tells the database to update the top-level test
field with the value you specify. But what you want to do is write a nested field, which takes using .
notation:
city_ref.update({"test.three" : 200})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论