mongoose search query on sub 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 search in multiple fields

let regex = new RegExp(value.searchQuery,'i');
const filterd = await Model.find({ $and: [ { $or: [{title: regex },{description: regex}] }, {category: value.category}, {city:value.city} ] })

Tags:

Misc Example