我需要找到在任何字段中包含子字符串的所有文档,
即文档:
{
a:"asd",
b:100,
c:
{
z:9
}
},
{
v:"asdfg",
p:"a100"
}
Run Code Online (Sandbox Code Playgroud)
因此,当我搜索"asd"或 时"10",我需要获取这两个文档。
我不知道这些文档中可以有哪些字段,所以我不能一一搜索。
我们在 DocumentDB 上有以ISO 8601 格式存储日期的文档。这些日期存储为字符串:
{
"CreatedOn": "2016-04-15T14:54:40Z",
"Title": "Some title",
"id": "xxx-xxx-xxxx-xxx-xxx-xxx"
}
Run Code Online (Sandbox Code Playgroud)
我们在使用 ASP.NET Core 的 WebAPI上使用最新的Azure DocumentDB SDK (1.7.0) 。
映射文档的 C# 类具有字符串形式的“CreatedOn”属性。
public class Item
{
public string CreatedOn { get; set; }
public string id { get; set; }
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我们读取文档并且 SDK 对其进行反序列化时,它会尝试将其转换为DateTime,然后再转换回string。导致:
{
"CreatedOn": "15/04/2016 14:54:40",
"Title": "Some title",
"id": "xxx-xxx-xxxx-xxx-xxx-xxx"
}
Run Code Online (Sandbox Code Playgroud)
我需要的是 SDK 保持这些值不变。我尝试设置默认的 …
我想按标题的字母顺序对 DocumentDB 集合中的记录进行排序。起初我认为这是有效的:
SELECT c.Title FROM c ORDER BY c.Title
Run Code Online (Sandbox Code Playgroud)
但正如预期的那样,这会将小写字母排在大写字母之后。我希望我的搜索不区分大小写,所以我尝试了以下方法:
SELECT c.Title FROM c order by LOWER(c.Title)
Run Code Online (Sandbox Code Playgroud)
和这个:
SELECT LOWER(c.Title) AS title FROM c ORDER BY title
Run Code Online (Sandbox Code Playgroud)
但这都会产生错误。如何执行不区分大小写的字符串排序?
新的 Cosmos DB 图形 API 是否支持查询参数?例如在查询中:
IDocumentQuery<dynamic> query = client.CreateGremlinQuery<dynamic>(graph, "g.V().has('name', 'john')");
Run Code Online (Sandbox Code Playgroud)
我可以像在 DocumentDB 中那样用查询参数替换硬编码值“john”吗:
IQueryable<Book> queryable = client.CreateDocumentQuery<Book>(
collectionSelfLink,
new SqlQuerySpec
{
QueryText = "SELECT * FROM books b WHERE (b.Author.Name = @name)",
Parameters = new SqlParameterCollection()
{
new SqlParameter("@name", "Herman Melville")
}
});
Run Code Online (Sandbox Code Playgroud)
我是出于安全考虑才问的。或者是否有其他方法可以防御 Gremlin 中的注射?
此查询花费 265 RU/s:
SELECT top 1 * FROM c
WHERE c.CollectPackageId = 'd0613cbb-492b-4464-b66b-3634b5571826'
ORDER BY c.StartFetchDateTimeUtc DESC
StartFetchDateTimeUtc是一个字符串属性,使用 Cosmos API 序列化
此查询花费 5 RU/s:
SELECT top 1 * FROM c
WHERE c.CollectPackageId = 'd0613cbb-492b-4464-b66b-3634b5571826'
ORDER BY c._ts DESC
_ts是一个内置字段,一个基于 Unix 的数字时间戳。
结果示例(仅包含该字段和_ts):
"StartFetchDateTimeUtc": "2017-08-08T03:35:04.1654152Z",
"_ts": 1502163306
索引已就位,并遵循如何配置可排序字符串/时间戳的建议和教程。看起来像:
{
"path": "/StartFetchDateTimeUtc/?",
"indexes": [
{
"kind": "Range",
"dataType": "String",
"precision": -1
}
]
}
我正在 Azure 中使用 Cosmos DB 的 DocumentDB API。文档从 EventHub 获取,并由该 EventHub 触发的 Azure 函数存储到数据库中。
我保存的文档还包含几个枚举。不幸的是,这些枚举值是使用索引号而不是枚举值进行序列化的(请参见此处: https: //i.stack.imgur.com/3nP9o.png)。或者,更准确地说,这是该函数在 Azure 中运行时发生的情况。本地 IDE 中的测试运行良好。
这是写入 Cosmos/DocumentDB 的代码:
DocumentClient = new DocumentClient(new Uri(dbServiceEndpoint), dbAuthKey);
...
var response = DocumentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), tick);
response.Wait();
Run Code Online (Sandbox Code Playgroud)
“tick”是提到的要存储的对象。
我尝试了一些方法来使文档客户端正确序列化枚举:
枚举属性的注释:
[Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
public OrderActionEnum? Action { get; set; }
Run Code Online (Sandbox Code Playgroud)枚举定义处的注释:
[Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
public enum OrderActionEnum
{
NEW,
CHANGE,
DELETE,
SNAPSHOT,
INITIAL
}
Run Code Online (Sandbox Code Playgroud)将转换器传递给文档数据库客户端的构造函数:
DocumentClient = new DocumentClient(new Uri(dbServiceEndpoint), dbAuthKey,
new JsonSerializerSettings
{
Converters = new List<JsonConverter> {new StringEnumConverter()}
});
Run Code Online (Sandbox Code Playgroud)设置默认序列化器: …
我有一个文档数据库存储库类,它有一个 get 方法,如下所示:
private static DocumentClient client;
public async Task<TEntity> Get(string id, string partitionKey = null)
{
try
{
RequestOptions requestOptions = null;
if (partitionKey != null)
{
requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKey) };
}
var result = await client.ReadDocumentAsync(
UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id),
requestOptions);
return (TEntity)(dynamic)result.Resource;
}
catch (DocumentClientException e)
{
// Have logic for different exceptions actually
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个集合 - Collection1 和 Collection2。Collection1 未分区,而 Collection2 已分区。
在客户端,我创建两个存储库对象,每个集合一个。
private static DocumentDBRepository<Collection1Item> collection1Repository = new …Run Code Online (Sandbox Code Playgroud) 您好,我有一个 Azure 流作业,它从事件中心获取数据并将其插入到 CosmosDB 数据库中。我想在插入数据时使用 GUID 作为 id,而不是文档中的值。我怎样才能在查询中实现这一点?
我们在 WebAPI 中有要求,以 JSON 的形式从外部 API 提取有效负载,清理并将其发布到 Azure Sql 中。对于此要求,我们目前依赖于 blob 存储,将 json 有效负载存储到 azure blob 中,并将其检索到 UI 中以进行数据清理活动。用户可以花费大量时间来验证数据并根据需要进行修改。用户可能会起草几天,并在完成所有清理后单击“导入”按钮。现在,我观察到,在这些草稿期间,blob 只是被检索并反序列化到对象列表中,以找到要更新的相应属性。一旦更新完成,当用户单击“草稿”时,同一列表将被序列化为 json 并存储回 blob。序列化/反序列化的过程似乎很昂贵。相反,我正在考虑用 Cosmos DB 替换 blob。这真的会即兴表演吗?建议 Azure Sql Json 支持是否比所有这些选项更可行?我什至想到了 Redis 缓存?决策的主要因素也是成本效益。
json azure azure-redis-cache azure-blob-storage azure-cosmosdb
我想使用 Mongo API 在我的 cosmoDB 实例上运行一个非常简单的 mongo 查询(使用 Robo 3T 作为 mongo 客户端)。
db.getCollection('events').aggregate(
[
{ $match: {customer: "printabled"}},
/*{ $count: "PEZZI_INC" }*/
{ $group: {
_id: null,
/*_id: { day: { $dayOfMonth: "$start_time"}},*/
totalCOUNTTEMP:{
$sum:"$AMBIENT_TEMPERATURE"
}
}
}
]
);
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误错误消息图片
如果链接不起作用,这里是文本错误。
Assert: command failed: {
"_t" : "OKMongoResponse",
"ok" : 0,
"code" : 50,
"errmsg" : "Request timed out.\r\nActivityId: c9fb8aa0-0000-0000-0000-000000000000, Microsoft.Azure.Documents.Common/1.22.0.0",
"$err" : "Request timed out.\r\nActivityId: c9fb8aa0-0000-0000-0000-000000000000, Microsoft.Azure.Documents.Common/1.22.0.0"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
@(shell):1:1
Run Code Online (Sandbox Code Playgroud) azure-cosmosdb ×10
azure ×4
json ×2
async-await ×1
enums ×1
gremlin ×1
nosql ×1
sql ×1
streaming ×1