constcap={color:Yellow,size:'G',brand:Nike,_id:'123'}constcapDb=awaitCap.findById(cap._id)// capDb = { _id: objectId(''), color: Yellow,size: G, brand:Nike }// or constcapDb=awaitCap.findOne({color:Yellow,size:'G'})// capDb = { _id: objectId(''), color: Yellow,size: G, brand:Nike }// note: findOne will return the first document that matches the filter specified if an _id is not provided
Filter documents
// find only Yellow caps from the 'Nike' brand and model 'Special'interfacecap{brand:stringcolor:stringsize:stringmodel:string}constcaps=awaitCap.find({color:'Yellow'brand:'Nike',model:'Special'})
Filter documents matching array fields
// find the games which has 'PS5' platforminterfacegame{genre:stringtitle:stringplatforms:string[]}constgamesDb=[{_id:'1',genre:adventure,title:'God of War',platforms:['PS3','PS4','PS5']},{_id:'2',genre:adventure,title:'Demon souls remake',platforms:['PS5']},{_id:'2',genre:adventure,title:'GTA IV',platforms:['PS3']},{_id:'2',genre:adventure,title:'Forza horizon',platforms:['XBOX 360','XBOX ONE']}]constgames=awaitGame.find({platforms:'PS5'})/* games = [
{_id: '1', genre: adventure, title: 'God of War', platforms: ['PS3', 'PS4', 'PS5']},
{_id: '2', genre: adventure, title: 'Demon souls remake', platforms: ['PS5']},
]
*/
Filter documents matching array fields
// find the games which has 'PS5' and 'PS3' platforminterfacegame{genre:stringtitle:stringplatforms:string[]}constgamesDb=[{_id:'1',genre:adventure,title:'God of War',platforms:['PS3','PS4','PS5']},{_id:'2',genre:adventure,title:'Demon souls remake',platforms:['PS5']},{_id:'2',genre:adventure,title:'GTA IV',platforms:['PS3']},{_id:'2',genre:adventure,title:'Forza horizon',platforms:['XBOX 360','XBOX ONE']}]constgames=awaitGame.find({platforms:{$in:['PS3','PS5']})/*games = [
{_id: '1', genre: adventure, title: 'God of War', platforms: ['PS3', 'PS4', 'PS5']},
{_id: '2', genre: adventure, title: 'GTA IV', platforms: ['PS3']}
]
*/
Update object prop in array of objects field in document
// update a document where the monster arm size is 700 and set to 30 and tattoo to trueinterfacemonster{arms:[{size:number,tattoo:boolean,side:string}]}constmonster={_id:'1',arm:{size:700,tattoo:false,side:'left'}constmonsterUpdated=awaitMonster.findOneAndUpdate({'arms.side':'left'},{'arms.$.size':30,'arms.$.tattoo':true},{new:true})/* monsterUpdated = {
_id: '1',
arms: [{
size: 30,
tattoo: true,
side: 'left'
}]
*/
Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.
So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀
Top comments (2)
Great stuff Felipe! 👏
thanksss!!