[英]Query cosmos db document to filter documents
I am reading all documents from my cosmos db sql api collection and I would like to filter out those documents which has a specific property defined in the json.我正在读取我的 cosmos db sql api 集合中的所有文档,我想过滤掉那些在 json 中定义了特定属性的文档。 So if documents are of this schema所以如果文档是这种模式
{
id: 1
name: aaa
project: ssdf
}
I would like to filter out documents which has "project" property in it.我想过滤掉包含“项目”属性的文档。 My code till now where I am able to read documents from collection:到目前为止,我的代码可以从集合中读取文档:
do
{
var feed = await client.ReadDocumentFeedAsync(
UriFactory.CreateDocumentCollectionUri( SourceDatabase, SourceCollection ),
new FeedOptions { MaxItemCount = 10, RequestContinuation = continuationToken } );
continuationToken = feed.ResponseContinuation;
foreach( Document document in feed )
{
Console.WriteLine( document );
}
}
The query by which I get filtered documents in Cosmos db is select * FROM c WHERE IS_DEFINED(c.Project)
.我在 Cosmos db 中获取过滤文档的查询是select * FROM c WHERE IS_DEFINED(c.Project)
。 How do I do it in code above?我如何在上面的代码中做到这一点?
Updating Question after Gaurav's answer:在 Gaurav 回答后更新问题:
static async Task Main()
{
string continuationToken = null;
DocumentClient client = new DocumentClient( new Uri( endpointUrl ), authorizationKey );
var feed = client.CreateDocumentQuery(
UriFactory.CreateDocumentCollectionUri( SourceDatabase, SourceCollection ).ToString(),
new SqlQuerySpec( "select * FROM c WHERE IS_DEFINED(c.Project)" ),
new FeedOptions { MaxItemCount = 100, EnableCrossPartitionQuery=true } ) ;
continuationToken = feed.
foreach( Document document in feed )
{
Console.WriteLine( document.Id );
}
I believe you're using version 2.0 of the Cosmos DB SDK.我相信您使用的是 Cosmos DB SDK 2.0 版。 If that's the case, then you can use CreateDocumentQuery(String, String, FeedOptions)
method to execute a query.如果是这种情况,那么您可以使用CreateDocumentQuery(String, String, FeedOptions)
方法来执行查询。
Here's an example to do the same:这是一个执行相同操作的示例:
// SQL querying allows dynamic property access
dynamic document = client.CreateDocumentQuery<dynamic>(collectionLink,
"SELECT * FROM books b WHERE b.title == 'War and Peace'").AsEnumerable().FirstOrDefault();
It is highly recommended that you start using SDK version 3.0
as all the new features are/will be available in the new SDK.强烈建议您开始使用SDK version 3.0
因为所有新功能都/将在新 SDK 中可用。
UPDATE更新
Please see code below.请看下面的代码。 It fetches 100 documents from collection at a time.它一次从集合中获取 100 个文档。
using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
namespace SO68439632
{
class Program
{
static void Main(string[] args)
{
var endpointUrl = "https://accountname.documents.azure.com:443/";
var authorizationKey = "authorizationkey==";
DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
var databaseId = "database-name";
var containerId = "container-name";
string continuationToken = null;
string collectionUrl = UriFactory.CreateDocumentCollectionUri(databaseId, containerId).ToString();
do
{
var feed = client.CreateDocumentQuery(
collectionUrl,
new SqlQuerySpec("select * FROM c WHERE c.Level = 2"),
new FeedOptions { MaxItemCount = 100, EnableCrossPartitionQuery = true, RequestContinuation = continuationToken }).AsDocumentQuery();
var result = feed.ExecuteNextAsync().Result;
continuationToken = result.ResponseContinuation;
var itemsCount = result.Count;
Console.WriteLine(string.Format("Total items fetched: {0}; More results available: {1}", itemsCount, !string.IsNullOrWhiteSpace(continuationToken)));
}
while (continuationToken != null);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.