查询嵌套文档和根级别

查询嵌套文档和根级别

本文介绍了DocumentDb-查询嵌套文档和根级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提醒我|编辑删除|变更类型问题您无法对自己的帖子投票0嗨!

Alert me | Edit | Delete | Change typeQuestionYou cannot vote on your own post0Hi!

让我们假设我有以下文件:

Lets assume I have docuents in following manner:

{
  id: 123,
  tags: [ { name: "something" } ]
}

,我想查询所有包含名称为"searched"或ID为9000的标签的文档.我在游乐场( https://www.documentdb.com/sql/demo )上进行了测试:

and I want to query all documents that contain a tag with name="searched" OR have the id=9000. I tested on playground ( https://www.documentdb.com/sql/demo )something like:

SELECT food.id, food.description, food.tags
FROM food
JOIN tag IN food.tags
WHERE food.id = "09052" or tag.name="blueberries"

但是我得到了一堆重复的记录,食物中的每个文档都是该文档中标签数量的乘积.

but then I get a bunch of duplicate records, each document from food is times the amount of tags in that document.

在对嵌套集合和根属性进行过滤时,如何获得不同的结果?

How can I get distinct results when filtering on nested collection and root properties?

推荐答案

可能需要ARRAY_CONTAINS内置函数.请参阅 https://msdn.microsoft.com/library/azure/dn782250.aspx #bk_array_functions 以获得详细信息,例如:

The ARRAY_CONTAINS built-in function might be what you need. See https://msdn.microsoft.com/library/azure/dn782250.aspx#bk_array_functions for details, i.e., something like this:

SELECT food.id, food.description, food.tags
FROM food
WHERE food.id = "09052" or ARRAY_CONTAINS(food.tags, { "name": "blueberries" })

您可以在查询操场上测试此查询此处.

You can test this query on the Query Playground here.

请注意,该函数使用索引,因此理想情况下,当查询中还有另一个过滤器时,应使用该索引.否则,唯一的方法就是使用您之前的查询,然后在客户端执行"distinct".

Note that the function does not use the index, so ideally you should use this when there's another filter in the query. Otherwise, the only way to do this is to use the query you had previously, then perform a "distinct" on the client side.

这篇关于DocumentDb-查询嵌套文档和根级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:58