The Power of Dot Notation: Querying Arrays and Sub-documents in MongoDB
In our previous lessons, we learned how to construct and compose filter documents, as well as query operators to express criteria other than simple equality. In this lesson, we're going to learn how to query arrays and sub-documents using dot notation. Dot notation is how MongoDB allows us to query document substructure, and it's a powerful tool for extracting specific data from our collections.
To demonstrate the power of dot notation, let's use the `findOne` method to retrieve one of my favorite laureates. Walter Cohn co-developed an important technique for computational chemistry. Notice that the prizes field is an array in this case, with only one element containing data on Cones to a share of the 1998 Prize in Chemistry. Additionally, a laureate may have many affiliations for a prize, and the affiliations field of each prize sub-document is an array.
To count the number of laureates who received a prize from Berkeley, California, we can use dot notation. We reach into the prizes array to query on the affiliations field across all prizes. From there, we reach again this time to query on the name field across affiliations. By using the dot notation, we can see that there are indeed laureates who had an affiliation in Berkeley, California when they received a prize.
One of the benefits of MongoDB is that it allows you to specify and enforce a schema for a collection, but this is not required. For example, fields do not need to have the same type of value across documents in a collection. In the case of this laureate, there's an empty field associated with this prize. Another accommodation in MongoDB is that of field presence; even root-level fields don't need to be present in all documents.
Using the `exists` operator, we can query for the existence or nonexistence of fields. For example, many laureates' documents do not have a born country field. But do all laureates have a prizes field? With the help of the `exists` operator, we see that they indeed do. However, are any of those prizes fields empty arrays? We can check using dot notation to access array elements, which borrows from JavaScript syntax.
To reference an array element by its numerical index, we use dot notation. For example, this filter document counts documents that have a non-empty prizes array. By seeing the results, we're relieved to know that all laureate documents contain at least one prize. But are there any laureates with more than one prize? Yes, we see that Marie Curie is in this group along with a few other people you may recognize.
In conclusion, dot notation is a powerful tool for querying arrays and sub-documents in MongoDB. By using it, we can extract specific data from our collections and perform complex queries. We've also learned about the `exists` operator to check for the presence of fields, which adds another layer of flexibility to our queries. With these tools, we can unlock the full potential of our MongoDB collection and perform more sophisticated queries.
One last thing to note is that while dot notation allows us to query arrays and sub-documents, it's not required for schema enforcement in MongoDB. In fact, fields do not need to have the same type of value across documents in a collection. This flexibility can be both beneficial and challenging, depending on how we choose to use it.
To demonstrate this point, let's consider an example where a laureate's document has a field that is empty for one prize but present for another. Using dot notation, we can query for the existence of specific fields across all documents in the collection. This allows us to perform complex queries without having to enforce a rigid schema.
In summary, dot notation is a powerful tool for querying arrays and sub-documents in MongoDB. By mastering it, we can unlock the full potential of our collections and perform more sophisticated queries. We've also learned about the `exists` operator to check for the presence of fields, which adds another layer of flexibility to our queries.
"WEBVTTKind: captionsLanguage: enin the last lesson we learned how to construct and compose filter documents we also learned about query operators to express criteria other than simple equality in this lesson we are going to learn how to query arrays and sub documents using dot notation dot notation is how MongoDB allows us to query document substructure let's use the find one method to retrieve one of my favorite laureates Walter Cohn co-developed an important technique for computational chemistry notice that the prizes field is an array in this case the array has one element with data on cones to a share of the 1998 Prize in Chemistry note also that a laureate may have many affiliations for a prize the affiliations field of each prize sub document is an array to fit text on this slide I use parentheses to form multi-line strings and I show only part of the document MongoDB allows you to query document substructure using dot notation here's a count of laureates we reach into the prizes array to query on the affiliations field across prizes from there we reach again this time to query on the name field across affiliations we count laureates with at least one prize affiliation named as specified the dot notation gives a full path to a field from the document route I'm curious how many laureates had an affiliation in Berkeley California when they received a prize here's my query go bears MongoDB allows you to specify and enforce a schema for a collection but this is not required for example fields do not need to have the same type of value across documents in a collection in the case of this laureate there's an empty of affiliation associated with this prize another accommodation in MongoDB is that of field presence even root level fields don't need to be present in all documents in this document for example the born country field is absent using the exists operator we can query for the existence or nonexistence of fields here we see that many laureates documents do not have a born country field do all laureates have a prizes field with the help of the exists operator we see that in fact they do but are any of those prizes fields empty arrays I hope not we can check using dot notation to access array elements this borrows from JavaScript syntax here we see a filter document for the criteria that a value exists for the fields 0 within the prizes field you can reference an array element by its numerical index using dot notation thus this expression counts documents that having non-empty prizes array we see to our relief that all laureate documents contain at least one prize are their laureates with more than one prize yes we see that Marie Curie is in this group along with a few other people you may recognize we've learned about dot notation to query array and sub document fields we've also learned about the exists operator to check for the presence of fields now let'sin the last lesson we learned how to construct and compose filter documents we also learned about query operators to express criteria other than simple equality in this lesson we are going to learn how to query arrays and sub documents using dot notation dot notation is how MongoDB allows us to query document substructure let's use the find one method to retrieve one of my favorite laureates Walter Cohn co-developed an important technique for computational chemistry notice that the prizes field is an array in this case the array has one element with data on cones to a share of the 1998 Prize in Chemistry note also that a laureate may have many affiliations for a prize the affiliations field of each prize sub document is an array to fit text on this slide I use parentheses to form multi-line strings and I show only part of the document MongoDB allows you to query document substructure using dot notation here's a count of laureates we reach into the prizes array to query on the affiliations field across prizes from there we reach again this time to query on the name field across affiliations we count laureates with at least one prize affiliation named as specified the dot notation gives a full path to a field from the document route I'm curious how many laureates had an affiliation in Berkeley California when they received a prize here's my query go bears MongoDB allows you to specify and enforce a schema for a collection but this is not required for example fields do not need to have the same type of value across documents in a collection in the case of this laureate there's an empty of affiliation associated with this prize another accommodation in MongoDB is that of field presence even root level fields don't need to be present in all documents in this document for example the born country field is absent using the exists operator we can query for the existence or nonexistence of fields here we see that many laureates documents do not have a born country field do all laureates have a prizes field with the help of the exists operator we see that in fact they do but are any of those prizes fields empty arrays I hope not we can check using dot notation to access array elements this borrows from JavaScript syntax here we see a filter document for the criteria that a value exists for the fields 0 within the prizes field you can reference an array element by its numerical index using dot notation thus this expression counts documents that having non-empty prizes array we see to our relief that all laureate documents contain at least one prize are their laureates with more than one prize yes we see that Marie Curie is in this group along with a few other people you may recognize we've learned about dot notation to query array and sub document fields we've also learned about the exists operator to check for the presence of fields now let's\n"