This content originally appeared on DEV Community and was authored by Felipe Leao
insert document
const cap = {
color: Yellow,
size: 'G',
brand: Nike
}
const capDb = await Cap.create(cap)
// capDb = { _id: objectId(''), color: Yellow,size: G, brand:Nike }
remove document
const cap = {
color: Yellow,
size: 'G',
brand: Nike,
_id: '123'
}
const capRemoved = await Cap.findByIdAndDelete(cap._id)
// capRemoved = { _id: objectId(''), color: Yellow,size: G, brand:Nike }
Find specific document
const cap = {
color: Yellow,
size: 'G',
brand: Nike,
_id: '123'
}
const capDb = await Cap.findById(cap._id)
// capDb = { _id: objectId(''), color: Yellow,size: G, brand:Nike }
// or
const capDb = await Cap.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'
interface cap {
brand: string
color: string
size: string
model: string
}
const caps = await Cap.find({color: 'Yellow' brand: 'Nike', model: 'Special'})
Filter documents matching array fields
// find the games which has 'PS5' platform
interface game {
genre: string
title: string
platforms: string[]
}
const gamesDb = [
{_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']}
]
const games = await Game.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' platform
interface game {
genre: string
title: string
platforms: string[]
}
const gamesDb = [
{_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']}
]
const games = await Game.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 true
interface monster {
arms: [{size: number, tattoo: boolean, side: string}]
}
const monster = {
_id: '1',
arm: {
size: 700,
tattoo: false,
side: 'left'
}
const monsterUpdated = await Monster.findOneAndUpdate(
{'arms.size': 700},
{'arms.$.size': 30, 'arms.$.tattoo': true},
{new: true}
)
/* monsterUpdated = {
_id: '1',
arms: [{
size: 30,
tattoo: true,
side: 'left'
}]
*/
This content originally appeared on DEV Community and was authored by Felipe Leao
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
Felipe Leao | Sciencx (2022-07-14T18:16:38+00:00) Basic operations every nodejs/mongoose developer should know. Retrieved from https://www.scien.cx/2022/07/14/basic-operations-every-nodejs-mongoose-developer-should-know/
" » Basic operations every nodejs/mongoose developer should know." Felipe Leao | Sciencx - Thursday July 14, 2022, https://www.scien.cx/2022/07/14/basic-operations-every-nodejs-mongoose-developer-should-know/
HARVARDFelipe Leao | Sciencx Thursday July 14, 2022 » Basic operations every nodejs/mongoose developer should know., viewed ,<https://www.scien.cx/2022/07/14/basic-operations-every-nodejs-mongoose-developer-should-know/>
VANCOUVERFelipe Leao | Sciencx - » Basic operations every nodejs/mongoose developer should know. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/14/basic-operations-every-nodejs-mongoose-developer-should-know/
CHICAGO" » Basic operations every nodejs/mongoose developer should know." Felipe Leao | Sciencx - Accessed . https://www.scien.cx/2022/07/14/basic-operations-every-nodejs-mongoose-developer-should-know/
IEEE" » Basic operations every nodejs/mongoose developer should know." Felipe Leao | Sciencx [Online]. Available: https://www.scien.cx/2022/07/14/basic-operations-every-nodejs-mongoose-developer-should-know/. [Accessed: ]
rf:citation » Basic operations every nodejs/mongoose developer should know | Felipe Leao | Sciencx | https://www.scien.cx/2022/07/14/basic-operations-every-nodejs-mongoose-developer-should-know/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.