“Mongoose Patch -Dokument” Code-Antworten

Mongoose finden und aktualisieren

var query = {'username': req.user.username};
req.newData.username = req.user.username;

MyModel.findOneAndUpdate(query, req.newData, {upsert: true}, function(err, doc) {
    if (err) return res.send(500, {error: err});
    return res.send('Succesfully saved.');
});
florinrelea

Mongoose speichern oder aktualisieren

// This will create another document if it doesn't exist
findByIdAndUpdate(_id, { something: 'updated' }, { upsert: true });
florinrelea

Mongoose Patch -Dokument

//Router file. After doing all your imports at beginning of file
const router = express.Router();
app.use('/pathforpostrequests', router);

router.patch('/:name', controller.updatePerson, (req, res, next) => {
  res.status(200).json(res.locals.person);
});

// Controller file. After doing all your imports at beginning of file. Person is an example mongo schema you will need to setup in a schema file.
const controller = {
  updatePerson (req, res, next) {
    Person.findOne({ firstName: req.params.name }).exec()
    .then((result) => {
      result.firstName = req.body.firstName;
      result.save();
      res.locals.person = result;
      next();
    })
  },
}
felinehasher

Ähnliche Antworten wie “Mongoose Patch -Dokument”

Fragen ähnlich wie “Mongoose Patch -Dokument”

Weitere verwandte Antworten zu “Mongoose Patch -Dokument” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen