google-cloud-dotnet icon indicating copy to clipboard operation
google-cloud-dotnet copied to clipboard

Firestore UpdateAsync with Nested Field Path (dot notation) Error

Open ryanmats opened this issue 8 years ago • 2 comments

I'm creating a code sample to show how to update nested fields using dot notation (https://cloud.google.com/firestore/docs/manage-data/add-data#update_fields_in_nested_objects):

// Assume the document contains: // { // name: "Frank", // favorites: { food: "Pizza", color: "Blue", subject: "recess" } // age: 12 // }

However, when creating my Dictionary<FieldPath, object> for updates to pass into the UpdateAsync function, I get this error.

Program.cs file - run using 'dotnet run update-nested-fields YOUR_PROJECT_ID'. AddData.csproj file Field Path Documentation DocumentReference.UpdateAsync Documentation

Should dot notation work for the FieldPath, or do I need to do something differently to update a nested field? Thanks!

ryanmats avatar Mar 19 '18 09:03 ryanmats

You're currently constructing a non-nested field path for a field that contains a dot.

If you update your code to

{ new FieldPath("Favorites", "Color"), "Red" },

that works. Note that in the current non-released code, you'd be able to specify the field names as string instead, and at that point dot-separated names would work:

Dictionary<string, object> updates = new Dictionary<string, object>
{
    { "Age", 13 },
    { "Favorites.Color", "Red" },
};

If you were updating a single field, you could do that without using a dictionary at all:

WriteResult updateResult = await frankDocRef.UpdateAsync("Age", 13);

Would it be useful for me to do a beta03 release with the current code? I'd been hoping to get the Listen API implemented before doing so, but it's easy enough to do. Note that there are a number of breaking changes (renamed methods, for the most part) so we'd need to fix any samples too. I can help with that.

jskeet avatar Mar 19 '18 09:03 jskeet

Thanks for the clarification.

I'm fine with holding off on releasing the dot-separated names feature until later.

ryanmats avatar Mar 19 '18 18:03 ryanmats