I'm new to Mongo and struggling to retrieve a specific Sub Document of a specific Document
Given the following collection, I want to retrieve the content where "id" = 1 and "data.id" = 2
. So, I'd want my result to be the "Java" object.
{
"id" : "1",
"title" : "Section1",
"data" : [
{
"id" : "1",
"Product" : "Scala",
"Description" : "",
},
{
"id" : "2",
"Product" : "Java",
"Description" : "",
},
{
"id" : "3",
"Product" : "Ruby",
"Description" : "",
},
{
"id" : "4",
"Product" : "HTML5",
"Description" : "",
}
]
}
{
"id" : "2",
"title" : "Section2",
"data" : [
{
"id" : "4",
"Product" : "Ansible",
"Description" : "a free software platform for configuring and managing computers",
},
{
"id" : "1",
"Product" : "Chef",
"Description" : "an open source configuration management tool",
},
{
"id" : "2",
"Product" : "Puppet",
"Description" : "an open-source tool for managing the configuration of computer systems",
},
{
"id" : "3",
"Product" : "Saltstack",
"Description" : "open source configuration management and remote execution application",
}
]
}
I'm executing a query like db.myCollection.find({$and : [{"id":"1"},{"data.id":"2"}]})
but this returns the whole document with an id of "1" rather than just the subdocument I'm interested in.
Am I asking Mongo to do something that it's not designed for, or is it just my syntax that is at fault?