R
R
RunFMe2015-03-15 18:30:43
JavaScript
RunFMe, 2015-03-15 18:30:43

How does findByIdAndUpdate work?

Doesn't find the record in the second case, everything works in the first one. Help me understand why.

app.post('/pari', function (req, res, next) {
    Pari.findById("55058fc4e9cbc78c1b9973d2", function(err, par){
       console.log(par); 
    });
        Pari.findByIdAndUpdate("55058fc4e9cbc78c1b9973d2", {date: Date.now, active: true}, function(err, curPari){
            console.log(curPari);
        });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor, 2015-03-15
@ByKraB

If I understood everything correctly, then you want to update the date and status for the document in the second case. For this you need to use "Update operators" mongodb. Your request should look something like this

Pari.findByIdAndUpdate("55058fc4e9cbc78c1b9973d2", {$set:{date: Date.now, active: true}}, function(err, curPari){
            console.log(curPari);
        });

You can read more in the documentation docs.mongodb.org/manual/reference/operator/update-field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question