Cosmos db联接给出重复的结果 [英] Cosmos db joins give duplicate results

查看:54
本文介绍了Cosmos db联接给出重复的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这样的文档结构:

{
  Id: "1",
  Title:"One",
  Children: [
    {Id:"A", Date:"01-01-2015"},
    {Id:"B", Date:"01-01-2016"},
  ]
}

我想获取所有子日期晚于"01-01-2014"的文档.我尝试过的查询是:

I want to get all documents that has a child date later than "01-01-2014". The query I've tried is:

SELECT parent.id, parent.Title
FROM parent 
JOIN child IN parent.Children
child.Date >= "01-01-2014"

这将给出重复的结果.有什么办法可以编写此查询,使其只返回不同的父母?

This will give a duplicate result. Is there any way to write this query so that it will only return distinct parents?

推荐答案

为什么不只在sql中使用distinct关键字来消除重复的记录? cosmos db支持.

Why don't just use distinct keyword in sql to eliminate duplicate records? It's supported by cosmos db.

SELECT distinct parent.id, parent.Title
FROM parent 
JOIN child IN parent.Children
where child.Date >= "01-01-2014"

希望它对您有帮助.

更新答案:

我在java sdk中测试了SqlQuerySpec,它工作正常.

I tested SqlQuerySpec in java sdk and it works fine.

        String collectionLink = String.format("/dbs/%s/colls/%s", "db", "coll");

        SqlQuerySpec sqlQuerySpec = new SqlQuerySpec();
        sqlQuerySpec.setQueryText("select distinct c.name from c");

        FeedResponse<Document> queryResults = documentClient.queryDocuments(
                collectionLink,
                sqlQuerySpec, queryOptions);

        System.out.println("Running SQL query...");
        for (Document document : queryResults.getQueryIterable()) {
            System.out.println(String.format("\tRead %s", document)); 
        }

我的SDK版本是:1.16.2(最新)

My Sdk version is :1.16.2(latest)

这篇关于Cosmos db联接给出重复的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆