0

I'm currently using the mongodb driver for node.js in a web project. I found the basic functions through w3schools and the mongo docs. But I haven't found a javascript method for validation, and the only way that I can't do it right now is through the mongo shell in cmd. Is there a function to write raw code in a js so that mongo can execute it like a script? That would save a lot of time having to deal with complex validations in console.

2
  • What are you trying to validate? Commented Oct 4, 2018 at 19:20
  • I'm validating schemas using the validator function for mongodb shell, using validator and $jsonSchema. I'm able to do it through command line, but I wanted to see if there was a higher level implementation. Commented Oct 5, 2018 at 16:30

3 Answers 3

1

I would highly suggest looking into mongoose npm module. Mongoose npm module has a lot of useful features that interact with MongoDB. You mentioned validating the data, which could also be found here.. As you are setting up schemas in Mongoose, you have the option to provide in-built validation methods.

Note this code is pulled from the validation page of mongoosejs.com

var breakfastSchema = new Schema({
    eggs: {
         type: Number,
         min: [6, 'Too few eggs'],
         max: 12
    },
    bacon: {
         type: Number,
         required: [true, 'Why no bacon?']
    },
    drink: {
         type: String,
         enum: ['Coffee', 'Tea'],
         required: function() {
             return this.bacon > 3;
         }
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I could be mistaken but I thought mongoose schemas only validate against themselves. I will keep on reading to see if I can do it with mongoose. What I want is to make use of the validator command for $jsonschema for mongo, 'cause that modifies the collection schema in mongo itself, and it's what I've been doing through mongo shell in cmd. Ty for your answer!
Mongoose docs say it's all middleware. Link: mongoosejs.com/docs/validation.html
@DavidCristerna so you are looking for a stand alone object to validate other objects? You could just try what you are doing in the terminal, but just with the mongodb library from npm. Or you could use Joi as another user mentioned in combination with the mongodb library or mongoose libarary.
0

So it sounds like what you are looking for is Stored JavaScript. While they are not equivalent, you can think of it almost like a Stored Procedure in SQL. Here is a good introduction to Stored JavaScript.

1 Comment

Not quite, but I will look into it too. Thanks for your answer!
0

If you are looking for validating the objects then you can use joi library.

1 Comment

Not quite, but I will look into it too. Thanks for your answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.