首页 > 解决方案 > 如何在 c# 上将子集合添加到 firestore?

问题描述

我正在尝试在 firestore 上的 c# 上的文档中添加子集合,但在 firestore 的文档页面中找不到它?

我是firestore和nosql的新手,很多这些东西对我来说仍然很复杂。

标签: c#google-cloud-firestore

解决方案


您只需在子集合中创建一个文档 - 集合本身不是您需要创建的实体。

所以如果你已经有一个DocumentReferencefor 的东西,你可以使用:

DocumentReference topDoc = ...;
CollectionReference subCollection = topDoc.Collection("subcollection");
DocumentReference subDoc = await subCollection.AddAsync(new { Name = "Jon", Score = 10 });

或者,如果您想指定一个 ID,请创建一个新 ID,DocumentReference然后从中创建文档:

DocumentReference topDoc = ...;
CollectionReference subCollection = topDoc.Collection("subcollection");
DocumentReference subDoc = subCollection.Document("subdoc");
await subDoc.CreateAsync(new { Name = "Jon", Score = 10 });

推荐阅读