Populating Queries with Mongoose | Creating a REST API with Node.js

preview_player
Показать описание
Sometimes, you have related documents in your MongoDB. Learn how you can populate them with data of each other via Mongoose.

----------

----------

See you in the videos!
Рекомендации по теме
Комментарии
Автор

Dude I've checked I don't know how many videos this afternoon and you're the only one clearly showing how to handle relationships in a non embedded way with MongoDB and easily get your full objects with Mongoose. Thanks.

asmodeos
Автор

As always you're one of the most helpful people on the internet. Even from a video from 2017 I learned everything I needed to get unstuck from my project. Thank you soooo much!

uskaishiga
Автор

Can't thank you enough for all these videos!

timmdunker
Автор

For anyone wondering how to specify the names of the fields you want to populate, just put a space between each field name; populate("property", "field1 field2 field3"). Even the documentation isn't clear about that.

hussam
Автор

Man you are a life saver i just discovered your vids and it so cleaning i will comeback to follow that series thanks a lot !

tkpcompshd
Автор

Thank you sir! Ive been finding the solution for that for hours now

elilumilay
Автор

My life was a joke without this video, it's 00:10 and I have to deliver this project today 8 o'clock, I wanted to die until this video, you're so f*cking amazing

zeit.geistt
Автор

hi Max, when returning early from a promise chain (when an order not found) using return statement, you are still executing all subsequent chains, therefore you are setting headers after response is set. Try throw 404 in the then branch, and then test for the status code in the catch block, then send the appropriate response in the catch block. Cheers.
.then(order) {
if(!order) => {
throw 404
} else {
...
}
}
.catch(err => {
if(err == 404){
"Model not found"})
} else {
res.status(500).json({error: err})
}
})

simonty
Автор

Im really grateful with this video, you've save me!!!

calebsolis
Автор

.select('_id product quantity')
.populate({
path: 'product', //ilgili koleksiyon
match: { price: { $gte: 1 }}, //eşleşme şartı price 1 den büyükse
select: '-_id name', //görünecek özellikler
options: { limit: 5 } //seçenekler
})
.exec()

MehmetALTINEL
Автор

I read in an article that it's better to organize my files around features, not roles.

Like this :-

Order directory that contains routes.js, model.js and controller.js files
Product directory that contains routes.js, model.js and controller.js files

With that, if you wanted to check Product files for instance, you won't have to open up three different directories, with lots of context switching. and you won't end up writing long paths when requiring modules.

markadell
Автор

ThankYou max please continue the series

shravanzolgikar
Автор

how to i filter using populate, say i wanted to find only the orders with a certain product name or id?

parumhr
Автор

Can we return a populated document after creating/posting?
example:
router.post('/' (req, res) => {

Order.create({someData})
.then(ord => res.status(201).json( ord.populate("product") )

});//end of .post

davidacosta
Автор

Hello i am trying populate query but it gives me error like that " Cast to ObjectId failed for value \"\" at path \"_id\" for model \"retailerrating\"
"

adnanahmed
Автор

But I found one problem with populate I have three fields where I want to populate eg. Company, ProductCategory and ProductSubCategory....the Company and ProductCategory are displaying but ProductSubCategory is displaying null please help me

bakorkharkongor
Автор

i'm so fkng love you right now men.! pfff all content that i need y just 3:24minutes

fladrian
Автор

Hi Max, populate method is not working correctly. It's return null value. Do you know why it's happening??

GaMaGe-qp
Автор

how do we make an update across multiple collections. What if there was a change in collection A which should also be updated in collection B?

jaggyjut
Автор

Thank you so much Maximillian.

I do have a question. I'm trying to build a one to many relation. One User, to Many Entries.

The issue I have is, when I make a post request to create a new entry, I can add the user ID to the request because a user already exists. But when I create a new user, no entries exist for that user, so even if I add the user ID to those entries, mongo db doesn't automatically populate those entries to the user object. I want to know what I need to do so mongo can dynamically update the user object, whenever I add a new entry to the target user.

Thank you so much

jahazielvazquez
welcome to shbcf.ru