将 Json 对象数组上传到 Firestore

Uploading a Json object array to Firestore

我有这个有序但很大的 Json 对象数组,它遵循以下结构:

{
   "object1":[
      {
         "data1":19.77,
         "data2":-0.953125,
         "data3":-0.265625,
         "id":17231,
         "date":"2021-05-14/08:38:47.471"
      },
      {
         "data1":19.77,
         "data2":-0.953125,
         "data3":-0.265625,
         "id":17231,
         "date":"2021-05-14/08:38:47.596"
      },
      {
         "data1":19.77,
         "data2":-0.9609375,
         "data3":-0.2734375,
         "id":17231,
         "date":"2021-05-14/08:38:47.721"
      }
   ],
   "object2":[
      {
         "data1":19.91,
         "data2":-0.9765625,
         "data3":-0.109375,
         "id":18996,
         "date":"2021-05-14/08:38:47.681"
      },
      {
         "data1":19.91,
         "data2":-0.9765625,
         "data3":-0.109375,
         "id":18996,
         "date":"2021-05-14/08:38:47.806"
      },
      {
         "data1":19.91,
         "data2":-1,
         "data3":-0.1171875,
         "id":18996,
         "date":"2021-05-14/08:38:47.931"
      },
      {
         "data1":19.91,
         "data2":-0.96875,
         "data3":-0.1015625,
         "id":18997,
         "date":"2021-05-14/08:38:48.051"
      },
      {
         "data1":19.91,
         "data2":-0.96875,
         "data3":-0.1015625,
         "id":18997,
         "date":"2021-05-14/08:38:48.059"
      },
      {
         "data1":19.91,
         "data2":-0.9765625,
         "data3":-0.109375,
         "id":18997,
         "date":"2021-05-14/08:38:48.176"
      }
   ]
}

我一直试图将它上传到 firebase 但没有成功,试图在 Firestore 中遵循这个模式:

Objects
    ├───Object1
    │   ├───[Upload Timestamp]
    │   │   └───Data Array 1
    │   ├───[Upload Timestamp]
    │   │   └───Data Array 2
    │   └───[Upload Timestamp]
    │       └───Data Array 3
    └───Object2
        ├───[Upload Timestamp]
        │   └───Data Array 1
        ├───[Upload Timestamp]
        │   └───Data Array 2
        ├───[Upload Timestamp]
        │   └───Data Array 3
        ├───[Upload Timestamp]
        │   └───Data Array 4
        ├───[Upload Timestamp]
        │   └───Data Array 5
        └───[Upload Timestamp]
            └───Data Array 6

我尝试使用嵌套的 For 循环、字典和诸如此类的东西来做到这一点,但没有成功。到目前为止,我只能将每个对象的最后一个数据数组上传到 Firestore。

有什么方法可以将其正确上传到 Firestore 吗?

编辑:添加有关我的代码的更多信息

我得到的:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import json
from itertools import groupby
import datetime
from datetime import date

today = date.today()
d1 = today.strftime("%d.%m.%Y")

json_file_path = "./myfile.json" 

with open(json_file_path, 'r') as j:
    contents = json.loads(j.read())
data = {}
#Sorting my json file
for key, items in groupby(sorted(contents, key = lambda x: (x['id'], x['date'])), key=lambda x: x['id']):  
    data[key] = list(items)

cred = credentials.Certificate("myAppCreds.json")

firebase_admin.initialize_app(cred, {
'projectId': "MyProjectID",
})

db = firestore.client()

for keys in data:
    doc_ref = db.collection('Objects').document(keys).collection(d1).document(str(datetime.datetime.now()))
    for info in data[keys]:
        doc_ref.set(keys)

会发生什么:

在 Firestore 中创建了以下条目:

Objects/object1/19.05.2021/2021-05-19 13:26:03.933868

文档“2021-05-19 13:26:03.933868”不断更新新值,而不是为每个新值创建一个新值。 object2

也是如此

这是一份工作草案,其中包含一些注释:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import json
from datetime import date

today = date.today()
d1 = today.strftime("%d.%m.%Y")

json_file_path = "./myfile.json" 

with open(json_file_path, 'r') as j:
    contents = json.loads(j.read())

# 1. This will be the criteria to sort each inner list
# first by ID, then by date, ascending
def _sort(item):
    return (item['id'], item['date'])

# 2. Sorting my json file lists, in place
for lista in contents.values():
    lista.sort(key=_sort)

cred = credentials.Certificate("myAppCreds.json")

firebase_admin.initialize_app(cred, {
'projectId': "MyProjectID",
})    

db = firestore.client()

# Inside the collection Objects, I create a document for each object key,
# Then set the inner list as an inner document, with value equals to the list.
for key, lista in contents.items():
    doc_ref = db.document('Objects/' + key).set({'lista': lista})

这不会完全按照 id/date 排序,因为对象是单独排序的。您可能需要对此进行调整。

在 firebase 中,我已成功创建文档 运行 此代码。