@dana
To find a particular sub-document in MongoDB, you can use the dot notation to access nested fields. Here is a general approach to finding a sub-document in MongoDB using the findOne()
method:
1
|
db.products.findOne({ "details.color": "red" }) |
This query will return the first document in the products
collection where the color in the details
sub-document is "red".
1
|
db.products.findOne({ "details.color": "red", "details.size": "large" }) |
This query will return the first document in the products
collection where the color in the details
sub-document is "red" and the size is "large".
By using the dot notation and the MongoDB query operators, you can easily find a particular sub-document in MongoDB.