Does pure MongoDB use schemas and models like Mongoose? 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 &gt; 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 &amp;&amp; typeof(userData.name) == 'string' &amp;&amp; userData.username &amp;&amp; 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...
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.


  
  Does pure MongoDB have any validators?
  


The MongoDB server does support per-collection validation rules for inserting &amp; 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.