Just imagine your document look like this:
[
{
"name": "Jess",
"location": "Auckland"
},
{
"name": "Dave",
"location": "Sydney"
},
{
"name": "Pete",
"location": "Brisbane"
},
{
"name": "Justin",
"location": "Auckland"
},
]
executing the following query;
myDB.find({location: 'Brisbane'})
will return:
[
{
"name": "Pete",
"location": "Brisbane"
}
]
While myDB.find({location: 'Auckland'}) will give you only those documents that contain the location as Auckland
[
{
"name": "Jess",
"location": "Auckland"
},
{
"name": "Justin",
"location": "Auckland"
},
]
As you can see, you're looking through the array for a key that matches the field you're looking to find and gives you back all of the documents that match that key search in the form of an array.
The Mongoose interface gives this data to you in the form of a callback and you just need to look for the item inside of the array it returns the following:
user.find({location: "Auckland"}, function(err, data){
if(err){
console.log(err);
return
}
if(data.length == 0) {
console.log("No record found")
return
}
console.log(data[0].name);
})