简体   繁体   English

firestore firebase 问题:如何在不指定文档名称的情况下更新文档

[英]firestore firebase problem: how to update the document without specifying the name of the document

在此处输入图像描述

I want to access UserPrefs and access the weather_location directly without specifying the document name.我想访问 UserPrefs 并直接访问 weather_location 而不指定文档名称。 How can I do that?我怎样才能做到这一点?

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

You can query the collection UserPrefs and find all the documents that contain a field "weather_location", then update all of them or only one.您可以查询集合 UserPrefs 并找到包含字段“weather_location”的所有文档,然后更新所有文档或仅更新一个。

something like:就像是:

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});

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

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