Mongoose: findOneAndUpdate doesn’t return updated document

Why this happens? The default is to return the original, unaltered document. If you want the new, updated document to be returned you have to pass an additional argument: an object with the new property set to true. From the mongoose docs: Query#findOneAndUpdate Model.findOneAndUpdate(conditions, update, options, (error, doc) => { // error: any errors that … Read more

What is the “__v” field in Mongoose

From here: The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. The name of this document property is configurable. The default is __v. If this conflicts with your application you can configure as such: new Schema({..}, { versionKey: ‘_somethingElse’ })

How do I update/upsert a document in Mongoose?

Mongoose now supports this natively with findOneAndUpdate (calls MongoDB findAndModify). The upsert = true option creates the object if it doesn’t exist. defaults to false. 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.’); }); In older versions Mongoose does … Read more

Find document with array that contains a specific value

As favouriteFoods is a simple array of strings, you can just query that field directly: PersonModel.find({ favouriteFoods: “sushi” }, …); // favouriteFoods contains “sushi” But I’d also recommend making the string array explicit in your schema: person = { name : String, favouriteFoods : [String] } The relevant documentation can be found here: https://docs.mongodb.com/manual/tutorial/query-arrays/

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)