I 've got an issue with mongoose schema validation, while trying to validate the schema of documents modified inside a Model::updateMany (or update + mutli:true) request.
I've got the schema below:
var BusinessesSchema = new Schema({
label: {
type: String,
required: true
},
openingDate: {
type: Date,
required: true
},
endingDate: {
type: Date,
validate: function(value) {
if (this.constructor.name === 'Query') {
// Looks like this is a validation for update request
var doc = null;
switch (this.op) {
case 'update':
case 'updateMany': {
doc = this._update.$set;
break;
}
case 'findOneAndUpdate': {
doc = this._update;
break;
}
default:
// keep null, will throw an error
}
return doc.openingDate < value;
}
else {
return this.openingDate < value;
}
}
}
});
I would like to access modified documents value ("this") inside endingDate::validate function to make sure that for each modified document ending Date is greater than beginning one .
Even, when using pre-hook (for update & updateMany, as below), I still do not find any way to access the modified documents value to perform my check when multi is set (or when calling updateMany).
BusinessesSchema.pre('update', function(next) {
this.options.runValidators = true;
this.options.context = 'query';
next();
});
BusinessesSchema.pre('updateMany', function(next) {
this.options.runValidators = true;
this.options.context = 'query';
next();
});
I probably missed something, and would really appreciate help here.
Can't do that. Best you can do with updateMany is to capture the query context, from this, and analyze the update. Something like this:
Schema.pre('updateMany', function (next) {
const update = this.getUpdate();
if (update.$set && update.$set && !validateUpdate(update.$set)) {
throw new Error('Invalid Update');
}
next();
});
If you wanna do validation on the resulting document before the update using the save hook, you can use a cursor:
Schema.pre('save', function(next) {
if (!validDoc(this)) {
throw new Error('Invalid Doc');
}
next();
}
Schema.statics.updateManyWithValidation = async function(criteria, update) {
const cursor = Model.find(criteria).cursor();
let doc = null;
do {
doc = await cursor.next();
Object.assign(doc, update);
await doc.save();
} while(doc != null);
}
Now, bear in mind this is a much more expensive operation since you're fetching the documents, applying the changes, and then saving them individually.
Related
I'am newbie to nodejs and mongodb, so how can I check if an object already exist in the collections , Note that my field type in the schema is object or JSON
const BillSchema = mongoose.Schema(
{
content: {
type: Object //or JSON
},
}
);
const Bill = module.exports = mongoose.model('Bill', BillSchema);
module.exports.addBill = function (newBill, callback) {
//Check for all bill titles and content, if newBill doesn't exist then add else do nothing
Bill.count({ content: newBill.content }, function (err, count) {
//count == 0 always ???
if (err) {
return callback(err, null);
} else {
if (count > 0) {
//The bill already exists in db
console.log('Bill already added');
return callback(null, null);
} else { //The bill doesnt appear in the db
newBill.save(callback);
console.log('Bill added');
}
}
});
}
One Of Nice Question You asked, I was suppose to achieve the same task before, I make the use of mongoose-unique-validator third party npm Package, & plugin to our schema
https://www.npmjs.com/package/mongoose-unique-validator
npm install mongoose-unique-validator
var uniqueValidator = require('mongoose-unique-validator');
const BillSchema = mongoose.Schema(
{
content: {type:Object , unique:true },
}
);
BillSchema.plugin(uniqueValidator, {message: 'is already taken.'});
Usage:
module.exports.addBill = function (newBill, callback) {
newBill.save(callback);
}
I Hope If this work for you too.
I was trying to create a single function to update different types of options in Mongoose model and encountered this strange behavior.
Here's what I was trying to do.
module.exports.updateUser = function(id, action, status, callback) {
const query = {
_id: id
};
let field = '';
switch (action) {
case 'download':
field = 'download_permission';
break;
case 'upload':
field = 'upload_permission';
break;
case 'view':
field = 'view_permission';
break;
case 'edit':
field = 'edit_permission';
break;
}
User.findOneAndUpdate(query, {
$set: {
field: status,
last_updated: moment().format('llll')
}
}, callback);
};
Now, if I try something like :
User.findOneAndUpdate(query, {
$set: {
'edit_permission': status,
last_updated: moment().format('llll')
}
}, callback);
};
It actually updates the document in the mongodb.
Can someone explain me why second works and not the first (doesn't update the document). Thanks.
In ES6, you can define computed keys using bracket notation.
User.findOneAndUpdate(query, {
$set: {
[field]: status,
last_updated: moment().format('llll')
}
}, callback);
User.findOneAndUpdate(query, {
$set: {
problem ------->>>>field<<<<<: status,
last_updated: moment().format('llll')
}
}, callback);
The field object param will not resolve to the value of your variable field.
This should fix it:
const setStatement = {
last_updated: moment().format('llll')
};
setStatement[field] = status;
User.findOneAndUpdate(query, {
$set: setStatement
}, callback);
I dont think you can have a variable as a key... If you put it that way... the variable itself becomes a string and becomes the key... the value of the variable wont appear in key's place
I am trying to create a service that can be used to update nested fields in a Mongoose model. In the following example I am trying to set the field 'meta.status' to the value 2. This is the service:
angular.module('rooms').factory('UpdateSvc',['$http', function($http)
{
return function(model, id, args)
{
var url = '/roomieUpdate/' + id;
$http.put(url, args).then(function(response)
{
response = response.data;
console.log(response.data);
});
}
}]);
This is how it is called in the controller:
var newStatus = {'meta.$.status' : 2};
var update = UpdateSvc("roomie", sessionStorage.getItem('userID'), newStatus);
And this is the model:
var RoomieSchema = new Schema(
{
meta:
{
created:
{
type: Date,
default: Date.now
},
status:
{
type: Number,
default: '1',
}
}
}
And this is the route:
app.put('/roomieUpdate/:id', function(req,res)
{
var id = req.params.id;
Roomie.findOneAndUpdate(
{_id: mongoose.Types.ObjectId(id)},
req.body,
{ new : true },
function(err, doc)
{
if(err)
{
console.log(err);
}
res.json(doc);
console.log(doc);
});
});
The argument is received correctly, but I can't seem to get this to work. I am not even getting an error message. console.log(doc) simply prints out the object and the field meta.status remains '1'. I have done a direct Mongo search on the target object to make sure that I wasn't just reading the old document. I've tried a great many things like separating the key and value of req.body and use {$set:{key:value}}, but result is the same.
findOneAndUpdate() by default will return the old document, not the new (updated) document.
For that, you need to set the new option:
Roomie.findOneAndUpdate({
_id : mongoose.Types.ObjectId(id)
}, req.body, { new : true }, function(err, doc) {
...
});
As it turns out, var newStatus = {'meta.$.status' : 2}; should have been var newStatus = {'meta.status' : 2}; The document now updates correctly.
The reason the $ was there in the first place was probably based on this thread:
findOneAndUpdate - Update the first object in array that has specific attribute
or another of the many threads I read through about this issue. I had tried several solutions with and without it, but couldn't get anything to go right.
How can I add helper methods to find and save objects in Mongoose. A friend told me to use helper methods but I cannot get them to work after a day. I always receive errors saying that either findOne() or save() does not exist OR that next callback is undefined (when node compiles ... before I execute it):
I've tried _schema.methods, _schema.statics... nothing works...
var email = require('email-addresses'),
mongoose = require('mongoose'),
strings = require('../../utilities/common/strings'),
uuid = require('node-uuid'),
validator = require('validator');
var _schema = new mongoose.Schema({
_id: {
type: String,
trim: true,
lowercase: true,
default: uuid.v4
},
n: { // Name
type: String,
required: true,
trim: true,
lowercase: true,
unique: true,
index: true
}
});
//_schema.index({
// d: 1,
// n: 1
//}, { unique: true });
_schema.pre('save', function (next) {
if (!this.n || strings.isNullOrWhitespace(this.n)){
self.invalidate('n', 'Domain name required but not supplied');
return next(new Error('Domain name required but not supplied'));
}
var a = email.parseOneAddress('test#' + this.n);
if (!a || !a.local || !a.domain){
self.invalidate('n', 'Name is not valid domain name');
return next(new Error('Name is not valid domain name'));
}
next();
});
_schema.statics.validateForSave = function (next) {
if (!this.n || strings.isNullOrWhitespace(this.n)){
return next(new Error('Domain name required but not supplied'));
}
var a = email.parseOneAddress('test#' + this.n);
if (!a || !a.local || !a.domain){
return next(new Error('Name is not valid domain name'));
}
next();
}
_schema.statics.findUnique = function (next) {
this.validateForSave(function(err){
if (err){ return next(err); }
mongoose.model('Domain').findOne({ n: this.n }, next);
//this.findOne({ n: this.n }, next);
});
}
_schema.statics.init = function (next) {
this.findUnique(function(err){
if (err){ return next(err); }
this.save(next);
});
}
var _model = mongoose.model('Domain', _schema);
module.exports = _model;
I believe you are running into issues because of your usage with this. Every time you enter a new function this's context is changing. You can read more about this at mdn.
Additionally your callbacks aren't allowing anything to be passed into the mongoose method. For example if I was to create the most basic "save" method I I would do the following:
_schema.statics.basicCreate = function(newDocData, next) {
new _model(newDocData).save(next);
}
Now if I wanted to search the Domain collection for a unique document I would use the following:
_schema.statics.basicSearch = function(uniqueName, next) {
var query = {n: uniqueName};
_model.findOne(query, function(err, myUniqueDoc) {
if (err) return next(err);
if (!myUniqueDoc) return next(new Error("No Domain with " + uniqueName + " found"));
next(null, myNewDoc);
});
}
Mongoose has built in validations for what you are doing:
_schema.path("n").validate(function(name) {
return name.length;
}, "Domain name is required");
_schema.path("n").validate(function(name) {
var a = email.parseOneAddress("test#" + name);
if (!a || !a.local || !a.domain) {
return false;
}
return true;
}, "Name is not a valid domain name");
It returns a boolean. If false, it passes an error to the .save() callback with the stated message. For validating uniqueness:
_schema.path("n").validate(function(name, next) {
var self = this;
this.model("Domain").findOne({n: name}, function(err, domain) {
if (err) return next(err);
if (domain) {
if (self._id === domain._id) {
return next(true);
}
return next(false);
}
return next(true);
});
}, "This domain is already taken");
You're using self = this here so that you can access the document inside the findOne() callback. false is being passed to the callback if the name exists and the result that is found isn't the document itself.
I've tried _schema.methods, _schema.statics
To clarify, .statics operate on the Model, .methods operate on the document. Zane gave a good example of statics, so here is an example of methods:
_schema.methods.isDotCom = function() {
return (/.com/).test(this.n);
}
var org = new Domain({n: "stuff.org"});
var com = new Domain({n: "things.com"});
org.isDotCom(); // false
com.isDotCom(); // true
Opinion: It's neat to have mongoose do validations, but it's very easy to forget it's happening. You also may want to have some validation in one area of your app while using different validations elsewhere. I'd avoid using most of it unless you definitively know you will have to do the same thing every time and will never NOT have to do it.
Methods/statics are a different story. It's very convenient to call isDotCom() instead of writing out a regex test every time you need to check. It performs a single and simple task that saves you some typing and makes your code more readable. Using methods for boolean checks can add a ton of readability. Defining statics like findByName (Zane's basicSearch) is great when you know you're going to do a simple query like that repeatedly.
Treat Mongoose as a utility, not as core functionality.
I have searched many questions on nested objects, but all I found where related to array[s].
I am looking for a updating simple nested object in mongoose.
From here http://mongoosejs.com/docs/guide.html
there is an example schema :
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
Once created a document,
How can I change the favs number later on?
There is no document for the same that I could find.
This is what I did:
blog.findById(entityId, function(err, mainDoc){
if(err || !mainDoc) return next(err || 'Document not found');
var subDoc = mainDoc['meta'];
if(subDoc){
subDoc = _.extend(subDoc, { favs : 56 }); //_ lib already available
console.log(mainDoc.get('meta')); //Prints the updated result with favs = 56 OK
mainDoc.save(function(err, doc){
console.log(doc.get('meta')); // prints the updated results with favs = 56 OK
});
} else next('Not found');
});
Everything works file and all console gives the desired result.
But when I switch to mongoose console and query the document, I do not get the updated result.
I know there can be other ways to achieve the same, but I am only looking for what I am doing wrong in this particular code.
Why the console, after saving document, gives unmatched data from database?
Upon enabling the mongoose debug option, I found the in query there is no such data to be updated. Query fires with blank $set. { $set : {} }
If you just want to change the value of favs, you can use a simpler query:
blog.findByIdAndUpdate(entityId, {$set: {'meta.favs': 56}}, function(err, doc) {
console.log(doc);
});
Hope I ain't late and will be able to help someone. This Works with deep nested objects as well. No limitations.
const updateNestedObjectParser = (nestedUpdateObject) => {
const final = {
}
Object.keys(nestedUpdateObject).forEach(k => {
if (typeof nestedUpdateObject[k] === 'object' && !Array.isArray(nestedUpdateObject[k])) {
const res = updateNestedObjectParser(nestedUpdateObject[k])
Object.keys(res).forEach(a => {
final[`${k}.${a}`] = res[a]
})
}
else
final[k] = nestedUpdateObject[k]
})
return final
}
console.log(updateNestedObjectParser({
a: {
b: {
c: 99
},
d: {
i: {
l: 22
}
}
},
o: {
a: 22,
l: {
i: "ad"
}
}
}))
The problem is that you can't do anything with data from mongoose once you've got it other than sending it to the client.
HOWEVER, there is the lean method that makes it so you can then update the info and do whatever you want with it.
That would look like this:
blog.findById(entityId).lean().exec(function (err, mainDoc) {
if (err || !mainDoc) {
return next(err || 'Document not found');
}
var subDoc = mainDoc.meta;
if(subDoc){
subDoc.favs = 56;
blog.update({_id: entityId}, mainDoc, function(err, doc){
console.log(doc.get('meta'));
});
} else {
next('Not found');
}
});