Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查集合和文档是否存在,如果不存在,如何使用.net在FireStore中创建具有特定ID的集合和文档_C#_.net_Google Cloud Firestore - Fatal编程技术网

C# 如何检查集合和文档是否存在,如果不存在,如何使用.net在FireStore中创建具有特定ID的集合和文档

C# 如何检查集合和文档是否存在,如果不存在,如何使用.net在FireStore中创建具有特定ID的集合和文档,c#,.net,google-cloud-firestore,C#,.net,Google Cloud Firestore,在Firestore中,您如何检查集合和文档是否已经存在,如果不使用.NET创建新集合和具有特定ID的文档?根据,集合将自动创建,但即使如此,您如何执行此操作仍不清楚,尤其是在.NET中。如果是树下的多个集合,则更是如此。例如,下面是我们将尝试添加的文档类: using Google.Cloud.Firestore; namespace FirestoreTest.Models { [FirestoreData] public class ClassicGame {

在Firestore中,您如何检查集合和文档是否已经存在,如果不使用.NET创建新集合和具有特定ID的文档?

根据,集合将自动创建,但即使如此,您如何执行此操作仍不清楚,尤其是在.NET中。如果是树下的多个集合,则更是如此。例如,下面是我们将尝试添加的文档类:

using Google.Cloud.Firestore;

namespace FirestoreTest.Models
{
    [FirestoreData]
    public class ClassicGame
    {
        public ClassicGame()
        {

        }

        [FirestoreProperty]
        public string title { get; set; }
        [FirestoreProperty]
        public string publisher { get; set; }
    }
}
下面是一个示例函数,用于检查集合和文档是否存在于PC/Games/{publisher}/{title}上,如果不存在,则使用gameId作为文档的ID创建它。它正在使用Google.Cloud.Firestore和Google.Cloud.Firestore.V1Beta1

public async Task AddPcGame(字符串gameTitle、字符串gamePublisher、字符串gameId)
{
string publisherCollectionPath=“PC/Games/”+gamePublisher;
//尝试获取文档并检查它是否存在
var document=await db.Collection(publisherCollectionPath).document(gameId.GetSnapshotAsync();
if(document.Exists)
{
//文档存在,请在此处执行所需操作。。。
返回true;
}
//如果不存在,请插入:
//创建要插入的对象
ClassicGame newGame=新ClassicGame()
{
标题=游戏标题,
publisher=gamePublisher
};
//注意,您必须遍历树,从集合到文档。
//如果你试图向前跳得太远,它似乎不起作用。
//.Document(gameId).SetAsync(newGame),是设置文档ID并插入文档数据的内容。
CollectionReference collection=db.collection(“PC”);
var document2=await collection.Document(“游戏”).collection(gamePublisher).Document(gameId).SetAsync(newGame);
返回true;
}
public async Task<bool> AddPcGame(string gameTitle, string gamePublisher, string gameId)
        {
            string publisherCollectionPath = "PC/Games/" + gamePublisher;

            //Try and get the document and check if it exists
            var document = await db.Collection(publisherCollectionPath).Document(gameId).GetSnapshotAsync();
            if (document.Exists)
            {
                //document exists, do what you want here...
                return true;
            }
             //if it doesn't exist insert it:  
            //create the object to insert
            ClassicGame newGame = new ClassicGame()
            {
                title = gameTitle,
                publisher = gamePublisher
            };
            //Notice you have to traverse the tree, going from collection to document.  
            //If you try to jump too far ahead, it doesn't seem to work. 
            //.Document(gameId).SetAsync(newGame), is what set's the document's ID and inserts the data for the document.
            CollectionReference collection = db.Collection("PC");
            var document2 = await collection.Document("Games").Collection(gamePublisher).Document(gameId).SetAsync(newGame);

            return true;
        }