last day (15 days later) » 

11:07 PM
0
Q: MongoDB node.js Update Function not Working

C. PortoI'm trying to update a whole document, but the update function does not complete and at the end of the request the server responds with a 504 http timeout. here's the function: var updateCallback = function (err, up_teacher) { if(err) throw err; res.send(200, {message : "Teacher ...

 
If teacher is a Mongoose doc it would be better to just call save on it.
 
Yes, but I would like to use multi, I think the save function does not have that option...
 
Okay, but that doesn't really make sense as you're finding the doc to update by its _id which guarantees this will only match a single doc.
 
Actually, doesn't it look for a teacher with _id inside the Teacher collection? I have the same teacher doc stored in an array inside another collection.
 
Yes, Teacher.update will only update docs in that model's collection.
 
11:07 PM
Yeah, that's why I need multi... strangely, locally this update is working, but not on my server. That's why I'm asking here if there's something wrong with my update.
 
The multi option doesn't make the update apply to multiple collections, it just makes the update apply to multiple documents in the Teacher collection.
 
11:22 PM
Oh guess I didn't understand then... although I'm almost certain it did work before. I've updated teacher from an array in other collection and it did update the same doc in Teacher collection. So, iss that impossible?
 
If that array in another collection is actually an array of ObjectIds references that you're populating during a find then yes, those populated teacher docs can be modified and saved and that will update the original doc in the teacher collection. But it's still just the one doc that gets updated.
 
Yes that's the case
So about my update function, is there anything unusual?
Something I might be doing wrong
 
Have you checked whether the errparameter is non-null?
 
in the callback function i check if(err) throw err;
Also, if I'm not wrong, the second parameter is the updated doc, is that correct?
 
Which means that your server would crash if you get an err, which would cause a timeout like you're seeing. Are you sure that's not happening?
No, the second parameter is the count of docs updated.
 
11:41 PM
that might be the case, I'll see if err is causing that, but I don't think so, what's causing the timeout is probably nginx, since I've set a specific timeout.
If err was causing that I think it would return immediately after doing the request, don't you think so?
 

  last day (15 days later) »