Skip to content

Instantly share code, notes, and snippets.

@ryanmats
Created March 19, 2018 09:37
Show Gist options
  • Save ryanmats/b075b76c2c854683be02a9c94a82f1e4 to your computer and use it in GitHub Desktop.
Save ryanmats/b075b76c2c854683be02a9c94a82f1e4 to your computer and use it in GitHub Desktop.
using CommandLine;
using Google.Cloud.Firestore;
using System;
using System.Collections;
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
update-nested-fields
";
private static async Task UpdateNestedFields(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_update_nested_fields]
DocumentReference frankDocRef = db.Collection("users").Document("frank");
Dictionary<string, object> initialData = new Dictionary<string, object>
{
{ "Name", "Frank" },
{ "Age", 12 }
};
Dictionary<string, object> favorites = new Dictionary<string, object>
{
{ "Food", "Pizza" },
{ "Color", "Blue" },
{ "Subject", "Recess" },
};
initialData.Add("Favorites", favorites);
WriteResult initialResult = await frankDocRef.SetAsync(initialData);
// Update age and favorite color
Dictionary<FieldPath, object> updates = new Dictionary<FieldPath, object>
{
{ new FieldPath("Age"), 13 },
{ new FieldPath("Favorites.Color"), "Red" },
};
// Asynchronously update the document
WriteResult updateResult = await frankDocRef.UpdateAsync(updates);
Console.WriteLine(updateResult.UpdateTime);
// [END fs_update_nested_fields]
Console.WriteLine("Updated the age and favorite color fields of the Frank document in the users collection.");
}
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 "update-nested-fields":
UpdateNestedFields(project).Wait();
break;
default:
Console.Write(Usage);
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment