使用 Azure Cosmos DB 的用于 MongoDB 的 API 查询数据Query data by using Azure Cosmos DB's API for MongoDB

Azure Cosmos DB 的用于 MongoDB 的 API 支持 MongoDB 查询The Azure Cosmos DB's API for MongoDB supports MongoDB queries.

本文涵盖以下任务:This article covers the following tasks:

  • 使用 MongoDB shell 查询 Cosmos 数据库中存储的数据Querying data stored in your Cosmos database using MongoDB shell

可以使用本文档中的示例来快速入门。You can get started by using the examples in this document.

示例文档Sample document

本文中的查询使用下面的示例文档。The queries in this article use the following sample document.

{
  "id": "WakefieldFamily",
  "parents": [
      { "familyName": "Wakefield", "givenName": "Robin" },
      { "familyName": "Miller", "givenName": "Ben" }
  ],
  "children": [
      {
        "familyName": "Merriam", 
        "givenName": "Jesse", 
        "gender": "female", "grade": 1,
        "pets": [
            { "givenName": "Goofy" },
            { "givenName": "Shadow" }
        ]
      },
      { 
        "familyName": "Miller", 
         "givenName": "Lisa", 
         "gender": "female", 
         "grade": 8 }
  ],
  "address": { "state": "NY", "county": "Manhattan", "city": "NY" },
  "creationDate": 1431620462,
  "isRegistered": false
}

示例查询 1Example query 1

在上述家庭示例文档中,以下查询返回其 ID 字段匹配 WakefieldFamily 的文档。Given the sample family document above, the following query returns the documents where the id field matches WakefieldFamily.

查询Query

db.families.find({ id: "WakefieldFamily"})

结果Results

{
"_id": "ObjectId(\"58f65e1198f3a12c7090e68c\")",
"id": "WakefieldFamily",
"parents": [
  {
    "familyName": "Wakefield",
    "givenName": "Robin"
  },
  {
    "familyName": "Miller",
    "givenName": "Ben"
  }
],
"children": [
  {
    "familyName": "Merriam",
    "givenName": "Jesse",
    "gender": "female",
    "grade": 1,
    "pets": [
      { "givenName": "Goofy" },
      { "givenName": "Shadow" }
    ]
  },
  {
    "familyName": "Miller",
    "givenName": "Lisa",
    "gender": "female",
    "grade": 8
  }
],
"address": {
  "state": "NY",
  "county": "Manhattan",
  "city": "NY"
},
"creationDate": 1431620462,
"isRegistered": false
}

示例查询 2Example query 2

下一个查询返回该家庭中的所有子女。The next query returns all the children in the family.

查询Query

db.families.find( { id: "WakefieldFamily" }, { children: true } )

结果Results

{
"_id": "ObjectId("58f65e1198f3a12c7090e68c")",
"children": [
  {
    "familyName": "Merriam",
    "givenName": "Jesse",
    "gender": "female",
    "grade": 1,
    "pets": [
      { "givenName": "Goofy" },
      { "givenName": "Shadow" }
    ]
  },
  {
    "familyName": "Miller",
    "givenName": "Lisa",
    "gender": "female",
    "grade": 8
  }
]
}

示例查询 3Example query 3

下一个查询返回所有已注册的家庭。The next query returns all the families that are registered.

查询Query

db.families.find( { "isRegistered" : true })

结果不返回任何文档。Results No document will be returned.

示例查询 4Example query 4

下一个查询返回所有未注册的家庭。The next query returns all the families that are not registered.

查询Query

db.families.find( { "isRegistered" : false })

结果Results

 {
"_id": ObjectId("58f65e1198f3a12c7090e68c"),
"id": "WakefieldFamily",
"parents": [{
    "familyName": "Wakefield",
    "givenName": "Robin"
}, {
    "familyName": "Miller",
    "givenName": "Ben"
}],
"children": [{
    "familyName": "Merriam",
    "givenName": "Jesse",
    "gender": "female",
    "grade": 1,
    "pets": [{
        "givenName": "Goofy"
    }, {
        "givenName": "Shadow"
    }]
}, {
    "familyName": "Miller",
    "givenName": "Lisa",
    "gender": "female",
    "grade": 8
}],
"address": {
    "state": "NY",
    "county": "Manhattan",
    "city": "NY"
},
"creationDate": 1431620462,
"isRegistered": false

}}

示例查询 5Example query 5

下一个查询返回所有未注册且所在州为纽约 (NY) 的家庭。The next query returns all the families that are not registered and state is NY.

查询Query

 db.families.find( { "isRegistered" : false, "address.state" : "NY" })

结果Results

 {
"_id": ObjectId("58f65e1198f3a12c7090e68c"),
"id": "WakefieldFamily",
"parents": [{
    "familyName": "Wakefield",
    "givenName": "Robin"
}, {
    "familyName": "Miller",
    "givenName": "Ben"
}],
"children": [{
    "familyName": "Merriam",
    "givenName": "Jesse",
    "gender": "female",
    "grade": 1,
    "pets": [{
        "givenName": "Goofy"
    }, {
        "givenName": "Shadow"
    }]
}, {
    "familyName": "Miller",
    "givenName": "Lisa",
    "gender": "female",
    "grade": 8
}],
"address": {
    "state": "NY",
    "county": "Manhattan",
    "city": "NY"
},
"creationDate": 1431620462,
"isRegistered": false

}}

示例查询 6Example query 6

下一个查询返回子女读 8 年级的所有家庭。The next query returns all the families where children grades are 8.

查询Query

 db.families.find( { children : { $elemMatch: { grade : 8 }} } )

结果Results

 {
"_id": ObjectId("58f65e1198f3a12c7090e68c"),
"id": "WakefieldFamily",
"parents": [{
    "familyName": "Wakefield",
    "givenName": "Robin"
}, {
    "familyName": "Miller",
    "givenName": "Ben"
}],
"children": [{
    "familyName": "Merriam",
    "givenName": "Jesse",
    "gender": "female",
    "grade": 1,
    "pets": [{
        "givenName": "Goofy"
    }, {
        "givenName": "Shadow"
    }]
}, {
    "familyName": "Miller",
    "givenName": "Lisa",
    "gender": "female",
    "grade": 8
}],
"address": {
    "state": "NY",
    "county": "Manhattan",
    "city": "NY"
},
"creationDate": 1431620462,
"isRegistered": false

}}

示例查询 7Example query 7

下一个查询返回有 3 个子女的家庭。The next query returns all the families where size of children array is 3.

查询Query

  db.Family.find( {children: { $size:3} } )

结果Results

将不返回任何结果,因为没有任何家庭的子女数超过 2。No results will be returned as there are no families with more than two children. 仅当参数为 2 时此查询才能成功,并返回完整的文档。Only when parameter is 2 this query will succeed and return the full document.

后续步骤Next steps

在本教程中已完成以下操作:In this tutorial, you've done the following:

  • 学习了如何使用 Cosmos DB 的用于 MongoDB 的 API 进行查询Learned how to query using Cosmos DB's API for MongoDB

现可继续学习下一教程,了解如何全局分发数据。You can now proceed to the next tutorial to learn how to distribute your data globally.