1

I'm new on pure MongoDB and I've some questions about pure MongoDB.


1. Does pure MongoDB use schemas and models like Mongoose ODM?

If you have used Mongoose ODM, you know that Mongoose ODM uses schemas and models for defining fields with their properties. Like this;

/* Dependencies */
const mongoose = require('mongoose');

/* Define the user schema */
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  lastname: {
    type: String,
    required: true
  }
});

/* Define the model */
const User = mongoose.model('User', userSchema);

Do we need to define schemas and models with their fields?

2. Does pure MongoDB have any validators?

For example, If we have a field in schema; we can define some properties to fields like;

...
  name: {
    type: String,  // Type built-in validator
    required: true // Required built-in validator
  },
  points: {
    type: Number,  // Type built-in validator
    min: 0,        // Min (minimum) built-in validator
    max: 100       // Max (maximum) built-in validator
  },
  verified: {
    type: Boolean, // Type built-in validator
    validate: {    // Custom schema validator
      validator: function(v) {
        return this.points > 50;
      },
      message: 'You can not register!'
    }
  }
...

Does pure MongoDB support for any validators or we define them while CRUD operations like this;

function createUser(userData, db, callback) {
  if(userData.name && typeof(userData.name) == 'string' && userData.username && typeof(userData.lastname) == 'string') { // Validator for object
    const collection = db.collection('users');
    collection.insertOne({
      name: userData.name,
      lastname: userData.lastname
    }, function(err, result) {
      if(err) callback(err, null);
      console.log('One record writed!');
      callback(null, result);
    });
  } else {
    callback(new Error('You must enter name and lastname to register!', null));
  }
}

All my questions are about these. Thanks...

1

MongoDB supports schema validation.

As per MongoDB official documentation for Schema Vaidation.

MongoDB provides the capability to perform schema validation during updates and insertions.

You can specify the rules when creating a collection or add validation to existing documents as well with validator option.

Example from MongoDB documentation.

db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "name", "year", "major", "gpa", "address.city", "address.street" ],
         properties: {
            name: {
               bsonType: "string",
               description: "must be a string and is required"
            },
            gender: {
               bsonType: "string",
               description: "must be a string and is not required"
            },
            year: {
               bsonType: "int",
               minimum: 2017,
               maximum: 3017,
               exclusiveMaximum: false,
               description: "must be an integer in [ 2017, 3017 ] and is required"
            },
            major: {
               enum: [ "Math", "English", "Computer Science", "History", null ],
               description: "can only be one of the enum values and is required"
            },
            gpa: {
               bsonType: [ "double" ],
               minimum: 0,
               description: "must be a double and is required"
            },
            "address.city" : {
               bsonType: "string",
               description: "must be a string and is required"
            },
            "address.street" : {
               bsonType: "string",
               description: "must be a string and is required"
            }
         }
      }
   }
})
  • Thank you for your excellent answer. I will give PLUS when I had 15 reputations. – Muhammed Ç. TUFAN Apr 17 at 20:35
  • Hey, I have two questions. Firstly, we can add Schema Validation to Collections when we are creating Collections. So, we will create only one collection in a project if we dont need one more collections. So, do we need to create operation files (.js) for creating Collections in our projects or we need to create or define Schema Validations other ways like MongoDB Compass or Mongo Shell? Question 2, can we change error messages in MongoDB? I have add an "unique index" to one field of my collection and i tried it with duplication. It gave me an error but I cant change the error message. – Muhammed Ç. TUFAN Apr 20 at 9:03
  • Please create this as a new question. – Mani Apr 20 at 20:54
1
  1. Does pure MongoDB use schemas and models like Mongoose ODM?

No, the MongoDB server (as at 4.0) does not have a concept of models or data relationships. Data is persisted in Documents which are stored on the server in a binary JSON-like serialization format called BSON.

  1. Does pure MongoDB have any validators?

The MongoDB server does support per-collection validation rules for inserting & updating documents. In MongoDB 3.6 validation rules are defined using JSON Schema; earlier versions used MongoDB query operators to express validation rules.

Mongoose vs MongoDB schema validation

Typically the goal is to validate data as early as possible in order to provide timely feedback to the end user and avoid unnecessary round trips and processing. For example, validation can happen in the frontend UI, in your application layer, and at the database layer. Frontend UI often has basic validation based on data types and field masks, while custom validation, relationships, and business logic is generally better suited to your application layer. Database validation is a final check to ensure only data fitting an expected (but flexible) schema is persisted.

Using a proposed standard like JSON Schema potentially allows validation rules to be shared by multiple layers of your stack. There are JSON Schema Implementations available for most popular programming languages and browsers. Note: MongoDB's implementation of JSON Schema has an Extension which is the inclusion of a bsonType keyword to allow use of all MongoDB BSON field types.

A current limitation of MongoDB's server-side JSON Schema validation is that it does not provide a detailed report of errors: documents either succeed or fail validation. You may want to watch/upvote SERVER-20547: Expose the reason an operation fails document validation in the MongoDB Jira issue tracker. Depending on your validation rules and options, failure will either result in an error (the insert/update is rejected) or a warning (the insert/update succeeds but results in a log message). Several of the JSON Schema implementations for application code have more detailed validation reporting suitable for end users.

  • Thank you for your excellent answer. I will give PLUS when I had 15 reputations. – Muhammed Ç. TUFAN Apr 17 at 20:35
1

Does pure MongoDB support for any validators or we define them while CRUD operations

As per MongoDB blog documentation here Traditionally, MongoDB developers have had to implement their own schema validation routines in their applications, but the arrival of JSON schema standards, the many JSON Schema validators and an excellent .NET validation library by NewtonSoft allowed this to be done in a standard way.

From Version 3.6, MongoDB supports this standard and provides a JSON Schema Validator from within the application that runs like a trigger to check any changes to the data via inserts or alterations.

A ‘Validation Schema’ had been introduced in 3.2, but 3.6 saw the introduction of the new standard way of doing this. Because it is an industry-standard, you can opt to validate your data from within MongoDB so that you can check data on entry or change, or you can export it as standard JSON, and validate it outside the database. Ideally, with bulk entry of data, you can validate it before import.

MongoDB is a document oriented database. which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents. For example

{
name : "sue",    <-----field:value
age  :  "26", <-----field:value
satatus: "A", <-----field:value
groups: ["news","sports"]  <-----field:value
}

As here The JSON document being validated or described we call the instance, and the document containing the description is called the schema.

The most basic schema is a blank JSON object, which constrains nothing, allows anything, and describes nothing:

{}

You can apply constraints on an instance by adding validation keywords to the schema. For example, the “type” keyword can be used to restrict an instance to an object, array, string, number, boolean, or null:

{ "type": "string" }

What JSON Schema checks for

The JSON Schema firstly determines how the data is stored. It is designed to check a JSON document, not a MongoDB collection, so we need to convert a ‘collection’ of documents into an array of documents.

However, if you provide a JSON Schema to a mongo collection, then it will work as if it is checking an array of objects, but you can configure the level of validation (validationLevel) and the action taken when an error is found (validationAction).

Out of the box, it will only check individual documents when they are altered or inserted.

The schema can do nothing if you want it to. The first thing it can check is the way that the data is structured within the document.

Normally, data is stored as arrays of objects, though tabular data can be stored as arrays of arrays.

It can then check on whether:

  • The object has all the required properties
  • Additional properties are allowed
  • There is at least a minimum number of properties
  • It is less than the maximum

You can define the data type of each property and how it should be validated to check for the range of values.

MongoDB schema validation in action

In order to check your validation, you need to take baby steps, and to do debugging in a high-quality JSON Schema Validator such as Newtonsoft’s browser-based validator that highlights the broken line and even explains why it failed.

enter image description here

There are plenty of ways that one can validate a property value. One concept that quickly comes up is that of a subschema. This can be as little as a JSON Schema empty object ‘{}’, but is normally a little more picky.

  • Thank you for your excellent answer. I will give PLUS when I had 15 reputations. – Muhammed Ç. TUFAN Apr 17 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.