azure documentdb 创建文档重复 Id 属性
azure documentdb create document duplicates Id property
我想在 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 使用的一些其他技术属性)。这里面装的是guid。
据我所知,通过存储一个已经有 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 属性 将被使用而不是自动生成的小写字母?
编辑:我可以确认在 Id
属性 上使用 [JsonProperty("id")]
时已解决此问题。但是任何已经插入的文档我无法检索 Id
的原始值
DocumentDB 的 id
属性 必须是独一无二的。您自己的 Id
属性(大写 I)不是特殊的 属性 - 它只是...您创建的 属性。
并且您已经在 id
属性 中看到了自动生成的 guid。请注意,您可以将自己的值放入 id
,只要它是唯一的即可。您不必接受默认值 (guid)。
ID 字段应为 "id" 小写。
这会解决它:
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
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
...
},
我想在 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 使用的一些其他技术属性)。这里面装的是guid。
据我所知,通过存储一个已经有 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 属性 将被使用而不是自动生成的小写字母?
编辑:我可以确认在 Id
属性 上使用 [JsonProperty("id")]
时已解决此问题。但是任何已经插入的文档我无法检索 Id
DocumentDB 的 id
属性 必须是独一无二的。您自己的 Id
属性(大写 I)不是特殊的 属性 - 它只是...您创建的 属性。
并且您已经在 id
属性 中看到了自动生成的 guid。请注意,您可以将自己的值放入 id
,只要它是唯一的即可。您不必接受默认值 (guid)。
ID 字段应为 "id" 小写。
这会解决它:
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
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
...
},