Search code examples
c#azureazure-cosmosdb

azure documentdb create document duplicates Id property


I want to store the following object in azure documentdb

public class MyDocument
{
    public string Id { get; set; }
    public SomeOtherClass SomeOtherClass { get; set; }
}

the property Id is a timestamp (yyyyMMddHHmmss) followed by a guid. As a side-effect, this allows me to sort on Id. I used this call to store it:

 await docDbClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(db, coll), myDoc);

When the document is stored, it has my documents properties (Id and SomeOtherClass) and an additional "id" property (note the lowercase "i"). (and some other technical properties used by documentdb). This is filled with a guid.

As far as I understood, by storing an object which already has an Id property, documentdb would use this and not generate it's own (but it did!).

Now I have two problems: I can't check with

 await docDbClient.ReadDocumentAsync(UriFactory.CreateDocumentCollectionUri(db, coll), myDoc.Id);

to see if a document already exists, and I can't use the Id property to sort and compare documents in a query like this:

IQueryable<MyDocument> query = _client.CreateDocumentQuery<MyDocument>(
            UriFactory.CreateDocumentCollectionUri(_account.Database, _account.Collection), queryOptions);
 query = query.Where(x => x.Id.CompareTo(inputValue) >= 0);

where inputValue is another key of the same form, to be used for paging. Because when deserializing the object out of documentdb, the id property replaces my Id property and I never see it's original value.

Am I using this wrong? Did I make the wrong assumption that my Id property would be used instead of an auto-generated one with lowercase?


EDIT: I can confirm that this is solved when using [JsonProperty("id")] on the Id property. But any document already insertedI can't retrieve my original value of Id


Solution

  • The ID field is expected to be "id" lower case.

    This would solve it:

    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }