排除Azure Cosmos DB中的路径

时间:2017-11-15 03:39:59

标签: azure-cosmosdb

从Azure CosmosDB中未将输入json中的某些键排除为未编入索引的正确JSON是什么。我们在mongodb模式下使用CosmosDB。计划在创建集合后更改Azure门户上的索引配置。

示例输入Json

{
    "name": "test",
    "age": 1,
    "location": "l1",
    "height":5.7
}

如果我要在索引中包含名称和年龄并从索引中删除位置和高度,那么includedPaths和excludedPaths是什么样的。

2 个答案:

答案 0 :(得分:4)

最后让它与下面的规范一起使用: -

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [{
        "path": "/*",
        "indexes": [{
                "kind": "Range",
                "dataType": "Number",
                "precision": -1
            },
            {
                "kind": "Hash",
                "dataType": "String",
                "precision": 3
            }
        ]
    }],
    "excludedPaths": [{
            "path": "/\"location\"/?"
        },
        {
            "path": "/\"height\"/?"
        }
    ]
}

答案 1 :(得分:0)

看来底层实现已更改,并且在撰写本文时,本文还没有涵盖MongoDB风格的CosmosBD中的indexPolicy更改。因为文档实际上是以有线方式存储的,其中所有键都从根$ v开始,并且所有标量字段都存储为包含值和类型信息的文档。因此,您的文档将被存储为:

{
  '_etag': '"2f00T0da-0000-0d00-0000-4cd987940000"',
  'id': 'SDSDFASDFASFAFASDFASDF',
  '_self': 'dbs/sMsxAA==/colls/dVsxAI8MBXI=/docs/sMsxAI8MBXIKAAAAAAAAAA==/',
  '_rid': 'sMsxAI8MBXIKAAAAAAAAAA==',
  '$t': 3,
  '_attachments': 'attachments/',
  '$v': {
    '_id': {
      '$t': 7,
      '$v': "\\Ù\x87\x14\x01\x15['\x18m\x01ú"
    },
    'name': {
      '$t': 2,
      '$v': 'test'
    },
    'age': {
      '$t': 1,
      '$v': 1
    },
    ...
  },
  '_ts': 1557759892
}

因此,indexingPolicy路径需要包含根$ v并使用/ *(对象)而不是/?。 (标量)。

{
  "indexingMode": "consistent",
  "automatic": true,
  "includedPaths": [
    {
      "path": "/*",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number"
        },
        {
          "kind": "Hash",
          "dataType": "String"
        }
      ]
    }
  ],
  "excludedPaths": [
    {"path": "/\"$v\"/\"location\"/*"},
    {"path": "/\"$v\"/\"height\"/*"}
  ]
}

PS: 也可以使用mongo api删除所有默认索引并根据需要创建特定索引 https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-indexing