简体   繁体   English

天蓝色documentdb创建文档重复ID属性

[英]azure documentdb create document duplicates Id property

I want to store the following object in azure documentdb 我想将以下对象存储在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. 属性ID是一个时间戳(yyyyMMddHHmmss),后跟一个GUID。 As a side-effect, this allows me to sort on Id. 作为副作用,这使我可以对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"). 存储文档时,它具有我的文档属性(Id和SomeOtherClass)和一个附加的"id"属性(请注意小写的“ i”)。 (and some other technical properties used by documentdb). (以及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!). 据我了解,通过存储已经具有Id属性的对象,documentdb将使用此属性而不生成它自己的属性(但是确实如此!)。

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: 来查看文档是否已经存在,并且我无法使用Id属性对这样的查询中的文档进行排序和比较:

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. 其中inputValue是另一种具有相同形式的键,用于分页。 Because when deserializing the object out of documentdb, the id property replaces my Id property and I never see it's original value. 因为当从documentdb中反序列化对象时, id属性将替换我的Id属性,而我再也看不到它的原始值。

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? 我是否错误地假设将使用我的Id属性代替小写的自动生成的属性?


EDIT: I can confirm that this is solved when using [JsonProperty("id")] on the Id property. 编辑:我可以确认在Id属性上使用[JsonProperty("id")]时已解决此问题。 But any document already insertedI can't retrieve my original value of Id 但是已经插入的任何文档我无法获取我的Id原始值

The ID field is expected to be "id" lower case. ID字段应为“ id”小写。

This would solve it: 这可以解决它:

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

DocumentDB's id property is what has to be unique. DocumentDB的id属性必须是唯一的。 Your own Id property (with upper-case I) is not a special property - it's just... a property that you created. 您自己的Id属性(大写的I)不是特殊属性-而是...您创建的属性。

And you've seen the guids auto-generated in the id property. 您已经在id属性中看到了自动生成的Guid。 Note that you can put your own value in id , as long as it's unique. 请注意,您可以将自己的值放在id ,只要它是唯一的即可。 You don't have to accept the default value (guid). 您不必接受默认值(guid)。

CosmosDb only guarantees unique id field values for the same partition key. CosmosDb仅保证同一分区键的唯一id字段值。 Therefore, this can exist in the same collection: 因此,这可以存在于同一集合中:

{
    "id": "12345",
    "myPartitionKey": "A",
    ...
},
{
    "id": "12345",
    "myPartitionKey": "B", //different partition key than above
    ...
},

but this cannot exist: 但这不存在:

{
    "id": "12345",
    "myPartitionKey": "A",
    ...
},
{
    "id": "12345",
    "myPartitionKey": "A", // Inconceivable! Same id AND partition key as above
    ...
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM