【问题标题】:firestore firebase problem: how to update the document without specifying the name of the documentfirestore firebase 问题:如何在不指定文档名称的情况下更新文档
【发布时间】:2021-04-24 11:00:50
【问题描述】:

我想访问 UserPrefs 并直接访问 weather_location 而不指定文档名称。我该怎么做?

const res = await firestore().collection('Users').doc(uid).collection('userPrefs').doc().set({weather_location: location});

【问题讨论】:

  • 该集合中有三个文档。如果您不知道 ID,那么您如何知道要更新哪些 ID?

标签: javascript firebase react-native google-cloud-firestore


【解决方案1】:

您可以查询集合 UserPrefs 并找到包含字段“weather_location”的所有文档,然后更新所有文档或仅更新一个。

类似:

const querySnapshot = await firestore()
    .collection('Users')
    .doc(uid)
    .collection('userPrefs')
    .where('weather_location', '>', '')
    .get();

const firstDoc = querySnapshot.docs[0];

await firestore()
    .collection('Users')
    .doc(uid)
    .collection('userPrefs')
    .doc(firstDoc.id)
    .set({weather_location: location});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2020-10-18
    • 2011-04-21
    相关资源
    最近更新 更多