Skip to content

Instantly share code, notes, and snippets.

@ryanmats
Last active March 16, 2018 20:36
Show Gist options
  • Save ryanmats/52535d2242881a58302e64c07f4e2dd3 to your computer and use it in GitHub Desktop.
Save ryanmats/52535d2242881a58302e64c07f4e2dd3 to your computer and use it in GitHub Desktop.
Program.cs File - Calling SnapshotAsync on Collection Reference
using CommandLine;
using Google.Cloud.Firestore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GoogleCloudSamples
{
public class QuickStartFirestoreProgram
{
public static string Usage = @"Usage:
C:\> dotnet run command project
C:\> dotnet run command my-project-id
Where command is one of
retrieve-all-documents
";
private static async void RetrieveAllDocuments(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
CollectionReference usersRef = db.Collection("users");
QuerySnapshot snapshot = await usersRef.SnapshotAsync();
foreach (DocumentSnapshot document in snapshot.Documents)
{
// Do anything you'd normally do with a DocumentSnapshot
Dictionary<string, object> documentDictionary = document.ToDictionary();
var firstName = documentDictionary["first"];
Console.WriteLine("First name is {0}", firstName);
}
}
public static void Main(string[] args)
{
if (args.Length < 2)
{
Console.Write(Usage);
return;
}
string command = args[0].ToLower();
string project = string.Join(" ",
new ArraySegment<string>(args, 1, args.Length - 1));
switch (command)
{
case "retrieve-all-documents":
RetrieveAllDocuments(project);
break;
default:
Console.Write(Usage);
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment