不知道对象键的Cosmos DB查询 [英] Cosmos DB query without knowing the key for object

查看:64
本文介绍了不知道对象键的Cosmos DB查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单个文档示例:

{
  "id" : "xxxxxx",
  "properties": {
      "a_prop": {
        type: "names",
        value: "John",
      },
      "b_prop": {
        type: "score",
        value: 5.5,
      },
      "c_prop": {
        type: "names",
        value: "Steve",
      }
   }
}

问题-如何获得具有至少一个类型为名称"的属性的文档??挣扎的是,在该属性"a_prop"之前,我不知道是一种名称".

Question - how can I get documents that has at least one property with type "names" ? Struggle is that I cannot know before that property "a_prop" is a type of "names".

推荐答案

当您的模型包含具有未知,任意键的对象集合时,进行查询就没有意思了.考虑将模型更改为具有已知键,并将未知值存储为属性值.例如,将 properties 更改为项目数组:

It's no fun to query when your model has collections of objects with unknown, arbitrary keys. Consider changing model to have known keys and store the unknowns as property values. For example, changing properties to an array of items:

{
  id: "xxxxxx",
  properties: [
    {
        name: "a_prop",
        type: "names",
        value: "John",
    },
    {
        name: "b_prop",
        type: "score",
        value: 5.5,
    },
    {
        name: "c_prop",
        type: "names",
        value: "Steve",
    }
  ]
}

现在基于数组的查询用于属性类型为名称"的项目:

Now the array-based query for items with a property of type "names":

SELECT * FROM c 
WHERE ARRAY_CONTAINS(c.properties, {"type": "names"}, true)

另请参阅此问题的讨论.

这篇关于不知道对象键的Cosmos DB查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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