Extracting collection/document path from document name
See original GitHub issueI often find myself in a situation where I have a Firestore document that I want to update or delete, but no other reference to the collection/name than Document.name, which doesn’t work as is, so I wrote custom functions to extract these parts from the name string. Is this really necessary? Is there a built-in solution in the library for doing this, or if not, should there be?
function pathToRef_(path) {
return path.match(/(?:[^\/]*\/|^)[^\/]*$/)[0];
}
const docs = fs.getDocuments("collection");
for (const doc in docs) {
const data = {
"key": value
}
fs.updateDocument(pathToRef_(docs[doc].name), data, true);
}
Library Version: 32
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Extract file name from path, no matter what the os/path format
WARNING: When os.path.basename() is used on a POSIX system to get the base name from a Windows-styled path (e.g. "C:\\my\\file.txt" ), the entire...
Read more >How Can I Extract Just the File Name from the Full Path to the ...
Here's a revised script that shows some of the other items that the FileSystemObject can extract from a file path:
Read more >Extract filename from path - TeX - LaTeX Stack Exchange
For labelling purposes I want to print the filename ...
Read more >How to list all subcollections of a Cloud Firestore document?
Retrieving a list of collections is not possible with the mobile/web client libraries. Fortunately, there are several possible workarounds to ...
Read more >Collection Methods | Documents | Data Model & Concepts
Query by examplePermalink. collection.byExample(example). Fetches all documents from a collection that match the specified example and returns a cursor.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Ok, I’ll be adding a property to the
Documentthat lets you get this data. In the meantime, you can use:Where the RegEx is from
Util_.regexPath.That would create a completely new collection hierarchy under
collection, becauseDocument.namereturns with the base path, likeprojects/project_id/databases/(default)/collection_name/document_name.