question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extracting collection/document path from document name

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
LaughDonorcommented, Jul 9, 2020

Ok, I’ll be adding a property to the Document that lets you get this data. In the meantime, you can use:

docs.forEach(doc => {
    const name = doc.name.match(/^projects\/.+?\/databases\/\(default\)\/documents\/(.+\/.+)$/)[1];
    fs.updateDocument(name, { "key": "newVal" }, true));
});

Where the RegEx is from Util_.regexPath.

1reaction
juliusncommented, Jul 9, 2020

Oh, I get it. Wouldn’t you be able to do:

const collectionPath = "collection";
const docs = fs.getDocuments(collectionPath);
docs.forEach(doc => fs.updateDocument(`${collectionPath}/${doc.name}`, { "key": "newVal" }, true));

to accomplish the same without the extraction process?

That would create a completely new collection hierarchy under collection, because Document.name returns with the base path, like projects/project_id/databases/(default)/collection_name/document_name.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found