An Interest In:
Web News this Week
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
- March 13, 2024
July 14, 2022 06:16 pm GMT
Original Link: https://dev.to/felipeleao18/operations-that-every-nodejsmongoose-developer-should-know-31f4
Basic operations every nodejs/mongoose developer should know
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: stringcolor: stringsize: stringmodel: string}const caps = await Cap.find({color: 'Yellow' brand: 'Nike', model: 'Special'})
Filter documents matching array fields
// find the games which has 'PS5' platforminterface game {genre: stringtitle: stringplatforms: 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' platforminterface game {genre: stringtitle: stringplatforms: 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 trueinterface 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'}]*/
Original Link: https://dev.to/felipeleao18/operations-that-every-nodejsmongoose-developer-should-know-31f4
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To