Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
APPLIES TO:
MongoDB
The Azure Cosmos DB's API for MongoDB supports MongoDB queries.
This article covers the following tasks:
- Querying data stored in your Azure Cosmos DB database using MongoDB shell
You can get started by using the examples in this article.
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
}
Given the sample family document, 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
}
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
}
]
}
The next query returns all the families that are registered.
Query:
db.families.find( { "isRegistered" : true })
Results:
No document is returned.
The next query returns all the families that aren't 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
}
The next query returns all the families that aren't 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
}
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
}
The next query returns all the families where size of children array is 3.
Query:
db.Family.find( {children: { $size:3} } )
Results:
No results are returned because there are no families with more than two children. Only when parameter value is 2
does this query succeed and return the full document.
In this tutorial, you've done the following tasks:
- Learned how to query using Azure Cosmos DB for MongoDB
You can now proceed to the next tutorial to learn how to distribute your data multiple-regionally.