将两个单独的具有相同名称的文档上传到Firebase

时间:2020-07-27 23:10:20

标签: ios swift firebase google-cloud-firestore

我希望能够在Firebase中添加两个具有相同名称的不同文档。现在,如果我尝试执行此操作,则第一个文档将被覆盖。这些文档具有不同的值。这是我目前的添加方式:

let batch = db.batch()

// update listCounter
let userRef = Firestore.firestore().collection("users").document(userID)
batch.updateData(["wishCounter": counter + 1], forDocument: userRef)

// save wishlist with properties
let wishRef = db.collection("users").document(userID).collection("wishlists").document(wishListName).collection("wünsche").document(wish.name)

batch.setData(["name": wish.name, "link": wish.link, "price": wish.price, "note": wish.note, "wishlistIDX": selectedWishlistIdx, "wishCounter": counter, "imageUrl": ""], forDocument: wishRef)
batch.commit { (error) in
    if let error = error {
        Utilities.showErrorPopUp(labelContent: "Wunsch konnte nicht gespeichert werden", description: error.localizedDescription)
    }
}

wishName应该始终被创建。我不知道更多代码是否对您有所帮助,但请告诉我是否有帮助。在此上找不到任何东西,所以我对每一个帮助都很满意!

1 个答案:

答案 0 :(得分:2)

两个文档在同一集合中不可能具有相同的ID。集合中的ID必须唯一。我建议重新考虑数据库模型,并提出一种无需重复ID即可对数据建模的方法。通常,您接受import time import geobuf from netCDF4 import Dataset from collections import OrderedDict def main(): ''' The main method that holds the logic for conversion ''' dataset = Dataset('outfile.nc', 'r') data_dict = OrderedDict({ 'type': 'FeatureCollection', 'features': [] }) for traj in range(dataset.variables['trajectory'].size): for t in range(dataset.variables['time'].size): lat = dataset.variables['lat'][traj, t].tolist() lon = dataset.variables['lon'][traj, t].tolist() if lat is None or lon is None: continue feature_dict = OrderedDict({ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [lat, lon] }, 'properties': {} }) for var in dataset.variables.keys(): if var not in ['lon', 'lat', 'time', 'trajectory']: dvar = dataset.variables[var] feature_dict['properties'][var] = dvar[traj, t].tolist() data_dict['features'].append(feature_dict) # This encoding is not slow pbf = geobuf.encode(data_dict) print(pbf) dataset.close() if __name__ == '__main__': ''' Entrypoint to this script ''' start_time = time.time() main() print("--- %s seconds ---" % (time.time() - start_time)) 创建的随机ID,并依靠字段值来查询匹配的文档。