mongoose find nested object code example

Example 1: finding by sub property of an object in mongo

db.messages.find( { headers : { From: "[email protected]" } } )

This queries for documents where headers equals { From: ... }, i.e. contains no other fields.

db.messages.find( { 'headers.From': "[email protected]" }  )

This only looks at the headers.From field, not affected by other fields contained in, or missing from, headers.

Example 2: mongoose find get nested prop only

ListModel.findOne({
	"_id" : "57e6bcab6b383120f0395aed", 'recipients.status' : 1
  },
  {
  	_id:1, name: 1, subject:1,'recipients.$': 1
   },
   function(err,list) {...}
);

Example 3: mongoose select nested

var fields = { 'properties.OBJECTID': 1, 'properties.TIMESTAMP': 1 };
var query = Feature.find({id: 1}).select(fields);

Example 4: mongoose find by nested property

System.findOne({ 'nodes.main.Alpha': 23000}, function(err, system){
   if(err){console.log(err);}
   else{console.log(system);}
 });

Tags:

Misc Example