如何在 Java 中创建 SqlQuerySpec 以检索具有给定 ID 的文档列表(where-in 子句)
How to create the SqlQuerySpec in Java in order to retrieve a list of documents with given ids (where-in clause)
我正在使用 SQL Api 开发 Azure Cosmos DB。我正在使用来自:
的 Azure SDK
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.7</version>
</dependency>
我有一个 ids 的列表,我想从我的列表中找到所有带有 ids 的文档。为了实现这一点,我使用 Azure SDK SqlQuerySpec,我在其中定义了一个 "WHERE IN" 查询,如下所示:
List<String> ids = Lists.newArrayList("1");
SqlQuerySpec spec = new SqlQuerySpec(
"SELECT * FROM MyLog c WHERE c.id IN (@ids)",
new SqlParameterCollection(new SqlParameter("@ids", ids)));
FeedResponse<Document> documentFeedResponse = documentClient.queryDocuments(collectionLink, spec, queryOptions);
List<Document> documents = documentFeedResponse.getQueryIterable().toList();
但不幸的是 "documents" 列表是空的,尽管在我的数据库中我有一个 id=1 的文档。只是为了仔细检查我已经尝试 运行 门户中的查询:
SELECT * FROM c where c.id IN ("1")
它 returns 数据正确。所以我不确定我做错了什么。
有人已经创建了 SqlQuerySpec 以检索具有给定 ID 的文档列表吗?
其实IN
和where c.id IN ("1")
一样使用,参数不是数组
您可以使用 Array_Contains
来实现您的需求:
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
SqlQuerySpec spec = new SqlQuerySpec(
"SELECT * FROM c WHERE array_contains(@ids, c.id )",
new SqlParameterCollection(new SqlParameter("@ids", ids)));
FeedResponse<Document> documentFeedResponse = documentClient.queryDocuments(collectionLink, spec, feedOptions);
List<Document> documents = documentFeedResponse.getQueryIterable().toList();
输出:
我正在使用 SQL Api 开发 Azure Cosmos DB。我正在使用来自:
的 Azure SDK<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.7</version>
</dependency>
我有一个 ids 的列表,我想从我的列表中找到所有带有 ids 的文档。为了实现这一点,我使用 Azure SDK SqlQuerySpec,我在其中定义了一个 "WHERE IN" 查询,如下所示:
List<String> ids = Lists.newArrayList("1");
SqlQuerySpec spec = new SqlQuerySpec(
"SELECT * FROM MyLog c WHERE c.id IN (@ids)",
new SqlParameterCollection(new SqlParameter("@ids", ids)));
FeedResponse<Document> documentFeedResponse = documentClient.queryDocuments(collectionLink, spec, queryOptions);
List<Document> documents = documentFeedResponse.getQueryIterable().toList();
但不幸的是 "documents" 列表是空的,尽管在我的数据库中我有一个 id=1 的文档。只是为了仔细检查我已经尝试 运行 门户中的查询:
SELECT * FROM c where c.id IN ("1")
它 returns 数据正确。所以我不确定我做错了什么。 有人已经创建了 SqlQuerySpec 以检索具有给定 ID 的文档列表吗?
其实IN
和where c.id IN ("1")
一样使用,参数不是数组
您可以使用 Array_Contains
来实现您的需求:
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
SqlQuerySpec spec = new SqlQuerySpec(
"SELECT * FROM c WHERE array_contains(@ids, c.id )",
new SqlParameterCollection(new SqlParameter("@ids", ids)));
FeedResponse<Document> documentFeedResponse = documentClient.queryDocuments(collectionLink, spec, feedOptions);
List<Document> documents = documentFeedResponse.getQueryIterable().toList();
输出: