使用MongoDB API查询时如何检索DocumentDB ID?

时间:2018-01-30 02:14:06

标签: azure azure-cosmosdb

编辑:我们刚收到微软的回复称,不支持对“传统”documentdb实例使用mongodb api。我会将这个问题保持开放,直到有人提出自定义解决方案或Microsoft增强产品。

我们已使用mongodb API成功连接到现有的DocumentDB实例,但find()方法未在结果中返回id字段。

Query Tutorials page上的示例显示正在返回id字段,但它们还会显示"_id": "ObjectId(\"58f65e1198f3a12c7090e68c\")"字段,但不会解释如何生成58f6...字符串(它不会出现在页面顶部的示例文档中的任何位置。)

例如,我们可以使用查询find({"type":"attribute", "association.Season":"Spring"})

找到此文档
{
  "id": "ATTR-TEST-ATT-00007",
  "type": "attribute",
  "name": "Spring Football",
  "association": {
    "Season": "Spring"
  }
}

...但是mongodb API会从文档中省略id属性,所以我们只看到:

{
  "type": "attribute",
  "name": "Spring Football",
  "association": {
    "Season": "Spring"
  }
}

此外,如果我们使用find({"id": "ATTR-TEST-ATT-00001"}),即使在数据库中存在具有该ID的文档,也不会返回任何内容。

我们尝试使用project参数强制包含id字段,但它不起作用。

如果我们在文档中添加_id字段,那么mongodb驱动程序将返回_id字段而不需要投影,但这不是解决方案,因为我们希望同时使用DocumentDB SQL API和mongodb API同时。

{
  "id": "ATTR-TEST-ATT-00007",
  "_id": "ATTR-TEST-ATT-00007",
  "type": "attribute",
  "name": "Spring Football",
  "association": {
    "Season": "Spring"
  }
}

我们如何让mongodb API在查询中返回文档的id字段?

0 个答案:

没有答案
相关问题