Wednesday, 31 May 2017

MongoDB/Mongoose find update and save

This is my mongodb schema

var Client = new Schema({
    name: String,
    race: [{
        name: String,
        car: [{name: String}]
    }]
});

I want to find a car by _id, something like this:

Client.findOne({
    "race.car._id": req.body._id
}, {
    "race.$.car": 1
}, function(err, client) {

//How to get the right car ?

    client[ ? ].race[ ? ].car.name = "updated name";

//And sure, update the client
    client.save();

});

But i don't know the best way, for the findOne and for the save.



via visconti

No comments:

Post a Comment