0
votes

I want to insert documents to documentDb. This is working fine when I'm not adding a custom id property before inserting the doc.

Example

foreach (dynamic doc in docs)
{
    if (doc != null)
    {

        doc.id = Convert.ToString(doc.myCustomId); // myCustomId is an integer
        var addedDoc = await dbClient.UpsertDocumentAsync(collectionUri, doc);
        addedDocuments.Add(addedDoc);
    }
}

When I remove doc.id => the document is added with a guid as id.

The error is the following: "Can not convert Array to String."

"   bei Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)\r\n   bei Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)\r\n   bei Newtonsoft.Json.Linq.JToken.ToObject[T]()\r\n   bei Microsoft.Azure.Documents.JsonSerializable.GetValue[T](String propertyName)\r\n   bei Microsoft.Azure.Documents.Resource.get_Id()\r\n   bei Microsoft.Azure.Documents.Client.DocumentClient.ValidateResource(Resource resource)\r\n  

The code is executed in a Azure Function

1
What version of Newtonsoft.Json and the DocumentDb nuget package are you using? I think this may be an issue related to version mismatches. - brettsam
What is the value of doc.myCustomId? - Jesse Carter
@brettsam newtonsoft version is 10.0.3 and I have checked all deps in the project and every nuget packages references the same version 10.0.3 - elvir.dolic
@JesseCarter it an integer - elvir.dolic

1 Answers

3
votes

This error can occur if you use a version of Newtonsoft.Json higher than 9.0.1. Try moving everything back to that version and see if it works. I'd also recommend using the DocumentDB package 1.13.2 to be safe.

This happens because the host is compiled against a specific version of these packages and if you try to use a newer version you can get subtle breaks like this. See this issue for details on how we're looking to address this: https://github.com/Azure/azure-webjobs-sdk-script/issues/992.