我正在尝试查询我的 CosmosDB,然后遍历每个文档并从其中的属性中获取一个值以供使用,但由于某种原因,我只得到 null 而不是值
查询方法如下
private async Task<FeedResponse<Document>> FetchDocuments(string brand)
{
using (var client = new DocumentClient(new Uri(cosmosDBEndpointUrl), cosmosDBPrimaryKey))
{
FeedOptions queryOptions = new FeedOptions
{
MaxItemCount = 0,
PartitionKey = new PartitionKey(brand)
};
var query = client.CreateDocumentQuery<Document>(UriFactory.CreateDocumentCollectionUri(cosmosDBName, cosmosDBCollectionNameRawData), $"SELECT * from c where c.brand = \"{brand}\"", queryOptions).AsDocumentQuery();
var result = await query.ExecuteNextAsync<Document>();
return result;
}
}
然后我像这样循环遍历结果
var rawDataDocumentList = await FetchDocuments(brand);
foreach (var singleDoc in rawDataDocumentList)
{
string jongel = singleDoc.GetPropertyValue<string>("OriginalData.artno");
}
如果我打破循环,我可以看到我在那里有实际数据,但由于某种原因,它以 {{ }} 开始和结束,我期望一个 {} pr 也许我在这里遗漏了什么?好吧,我知道我错过了什么问题是因为 jongel 始终为空
我重写了我的方法如下,但同样的问题仍然存在
private async Task<List<String>> FetchDocuments(string brand)
{
using (var client = new DocumentClient(new Uri(cosmosDBEndpointUrl), cosmosDBPrimaryKey))
{
List<string> documentListInLoop = new List<string>();
FeedOptions queryOptions = new FeedOptions
{
MaxItemCount = -1,
PartitionKey = new PartitionKey(brand)
};
var query = client.CreateDocumentQuery<Document>(UriFactory.CreateDocumentCollectionUri(cosmosDBName, cosmosDBCollectionNameRawData), $"SELECT * from c where c.brand = \"{brand}\"", queryOptions).AsDocumentQuery();
while(query.HasMoreResults)
{
foreach (Document singleDocument in await query.ExecuteNextAsync<Document>())
{
string artNo = singleDocument.GetPropertyValue<string>("OriginalData.artno");
documentListInLoop.Add(Newtonsoft.Json.JsonConvert.SerializeObject(singleDocument.ToString()));
}
}
//var result = await query.ExecuteNextAsync<Document>();
return documentListInLoop;
}
}
如果我放一个断点,我可以看到 artno 属性,它看起来如下
{{
"brand": "XX",
"UpdatedAt": "2019-10-24T00:31:18",
"OriginalData": {
"id": "a2303ce5-bb28-4d90-90ad-f741327b416a",
"_id": "5da4eec9ee3b9100013f7e49",
"artno": "0697054056",
"vendor": "hm",
"updatedAt": "2019-10-22T22:02:01.365Z",
"locales": [
我把它剪掉以节省空间,你可以看到它以双尖括号开头,不知道为什么?
而且我无法为 artno 获得任何价值,即使它显然在那里,所以我必须以错误的方式访问,但我无法弄清楚我做错了什么。