azure documentdb创建文件重复Id属性

时间:2017-03-30 14:48:36

标签: c# azure azure-cosmosdb

我想将以下对象存储在azure documentdb

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

属性Id是时间戳(yyyyMMddHHmmss),后跟guid。作为副作用,这允许我对Id进行排序。 我用这个电话来存储它:

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

存储文档时,它具有我的文档属性(Id和SomeOtherClass)以及额外的"id"属性(请注意小写" i")。 (以及documentdb使用的一些其他技术属性)。这充满了指导。

据我所知,通过存储一个已经具有Id属性的对象,documentdb会使用它并且不会生成它自己(但确实如此!)。

现在我有两个问题:我无法检查

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

查看文档是否已存在,并且我无法使用Id属性对查询中的文档进行排序和比较,如下所示:

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

其中inputValue是同一表单的另一个键,用于分页。因为从documentdb反序列化对象时,id属性会替换我的Id属性,而我从未看到它的原始值。

我使用这个错了吗? 我是否做出了错误的假设,即我的Id属性将被使用,而不是使用小写的自动生成的属性?

编辑:我可以确认在[JsonProperty("id")]属性上使用Id时已解决此问题。但是已插入的任何文档都无法检索Id

的原始值

3 个答案:

答案 0 :(得分:4)

ID字段应为“id”小写。

这可以解决它:

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

答案 1 :(得分:2)

DocumentDB的id属性必须是唯一的。您自己的Id属性(大写字母I)不是特殊属性 - 它只是...您创建的属性。

您已经看到了id属性中自动生成的guid。请注意,您可以将自己的值放在id中,只要它是唯一的。您不必接受默认值(guid)。

答案 2 :(得分:0)

CosmosDb仅保证同一分区键的唯一id字段值。因此,它可以存在于同一集合中:

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

但这不存在:

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