warCommander
warCommander

Reputation: 449

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

enter image description here

I want to access UserPrefs and access the weather_location directly without specifying the document name. How can I do that ?

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

Upvotes: 0

Views: 42

Answers (1)

shapkar
shapkar

Reputation: 139

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.

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

Upvotes: 1

Related Questions