girlrockingguna
girlrockingguna

Reputation: 289

MongoDB not updating in node.js app?

I'm trying to update my database within the node.js app but it's not working. I console.log'ed the parameters and they are being shown correctly (i.e. brand, size, and user email are all valid and defined). How do I make this work?

Function within controller that's being called:

var User            = require('../models/user');

exports.addToFavs = function(req, res) {
    var user=req.user; 
    var brandToFind = req.body.brand;
    var size_result = req.body.size; 
    var email_user = user.local.email; 
    //res.send? local.email?
    User.update({local: {email: email_user}} , {$set: { $push: { favorite_shoes: { brand: brandToFind, size: size_result}}}} , {multi : true}, function(error) {
        if (error) console.log(error);   
        console.log('Added %s with size=%s', brandToFind, size_result);
        console.log(User.find(email_user)); 
        res.redirect('/favorites');
    })
}; 

Schema:

    var userSchema = mongoose.Schema({
    local            : {
        email        : String,
        password     : String,
    }, 
    favorite_shoes : [shoeSchema],
    history: [searchSchema],
});

var shoeSchema = mongoose.Schema({
    brand: String, 
    size: String, 
}); 

var searchSchema = mongoose.Schema({
    brand_original: String, 
    brand_result: String, 
    size: String,
});

Upvotes: 0

Views: 382

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151112

You have one glaring problem in your find and potentially another. This bit is wrong:

User.update({local: {email: email_user}}, ... 

And it's wrong because of the way you are specifying this it is looking for a document with local that exactly matches what you are querying. In other words, looking for a document that does not also have a password field in the sub-document as well. What you want is:

User.update({"local.email": email_user}, ...

That *will match an email field in the document with the matching value, regardless of what additional fields are in the sub-document. For more information, give the manual section a good read, until you are familiar with the various query forms.

http://docs.mongodb.org/manual/tutorial/query-documents/

The second potential thing that may not be immediately evident from the code you have posted is related to your use of Mongoose. It's a trap for the un-initiated and those still thinking in relational terms.

You show your schema definitions, but unless you are using only one model (just for User) then the operation you are trying to do will not work. The reason is, that in the single model form, then your related schema are considered to be embedded documents that are suitable for the update operations you are doing.

If you have however defined a model for each of shoeSchema and searchSchema, these documents will actually get created in separate collections and this type of update will not work.

In the latter case, go back to the documentation, get acquainted with the concepts, re-think and implement your logic again.

Upvotes: 1

Related Questions