在Firestore中提前生成和深度嵌套级别的集合ID?

huangapple go评论96阅读模式
英文:

Generating and deep nested level collection ID in advance in Firestore?

问题

I am trying to achieve creating the ID of a nested collection in advance the surfaceLevelDocId works fine and will generate an id, however, the second implementation should generate another id for the collection that will be nested below the first one (surfaceLevelDocId) but I am getting the following error

> Error FirebaseError: Invalid collection reference. Collection references must have an odd number of segments

the idea is I am setting all of those using a batch later on,
what I did try to add an argument that has nothing to do with anything like ('testData') it worked it gave me a random id but simply is not related to the real path by any means, and I am afraid that could have side effects (like duplicate ids later)
my point here is to generate an id for the newly nested collection that will adhere to the database ids

or something similar to

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const collectionRef = firebaseApp.firestore().collection("YOUR_COLLECTION_NAME");
const collectionId = collectionRef.generateId();

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// data-external/${uid}/data
const externalDoc = doc(fireStore, 'data-external/${uid}/');
// Important: Generated firebase docs ids up-front
const surfaceLevelDocId = doc(collection(externalDoc, 'data')).id;
const nestedLevelDocId = doc(collection(externalDoc, 'data', surfaceLevelDocId)).id;
console.log('nestedLevelDocId', nestedLevelDocId);

<!-- end snippet -->

英文:

I am trying to achieve creating the ID of a nested collection in advance the surfaceLevelDocId works fine and will generate an id, however, the second implementation should generate another id for the collection that will be nested below the first one (surfaceLevelDocId) but I am getting the following error

> Error FirebaseError: Invalid collection reference. Collection references must have an odd number of segments

the idea is I am setting all of those using a batch later on,
what I did try to add an argument that has nothing to do with anything like ('testData') it worked it gave me a random id but simply is not related to the real path by any means, and I am afraid that could have side effects (like duplicate ids later)
my point here is to generate an id for the newly nested collection that will adhere to the database ids

or something similar to

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const collectionRef = firebaseApp.firestore().collection(&quot;YOUR_COLLECTION_NAME&quot;);
const collectionId = collectionRef.generateId();

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// data-external/${uid}/data
 const externalDoc = doc(fireStore, &#39;data-external/${uid}/&#39;);
// Important: Generated firebase docs ids up-front
const surfaceLevelDocId = doc(collection(externalDoc, &#39;data&#39;)).id;
const nestedLevelDocId = doc(collection(externalDoc, &#39;data&#39;, surfaceLevelDocId)).id;
console.log(&#39;nestedLevelDocId&#39;, nestedLevelDocId);

<!-- end snippet -->

答案1

得分: 2

如@DougStevenson在他的评论中提到的,没有内置的功能可以帮助您为集合生成ID。唯一存在的函数叫做doc(),但它生成文档的ID,而不是集合的ID。除此之外,当涉及到查询时,您必须在事先知道集合/子集合的名称。使用随机名称来命名集合将使查询无法执行。您可能会想,我可以列出所有集合名称,然后进行查询,但在移动和Web客户端的情况下是不可能的。这只有在Node.js的admin客户端中才可能,您将能够找到一个listCollections()函数。

如果需要唯一性,那么您应该考虑使用唯一的ID将数据添加到文档中,因为这是Firestore设计的方式。如果这是要求,并且必须使用随机ID来命名集合名称,那么使用什么机制都无关紧要。可以是Firestore的机制,也可以是您自己的任何其他机制。然而,我建议不要这样做。

英文:

As @DougStevenson mentioned in his comment, there is nothing built in that can help you generate IDs for the collections. The only function that exists is called doc() but it generates IDs for documents, not for collections. Besides that, when it comes to queries, you have to know the names of the collections/sub-collections before time. Having random names for the collections will make the queries impossible to be performed. You might think, I can list all collection names and go ahead with the queries, but that is not possible when it comes to mobile and web clients. That is possible only with the Node.js admin clients where you'll be able to find a listCollections() function.

If you need uniqueness, then you should consider adding the data in documents using unique IDs, since that's the way Firestore is designed. If that's the requirement and it's mandatory to use random IDs for collection names, then it doesn't matter what mechanism you use. It can be Firestore's mechanism or any other mechanism of yours. However, I recommend against it.

huangapple
  • 本文由 发表于 2023年8月5日 03:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定