How to validate (and check) Mongoose ID references

When I use mongoose for my projects I usually have documents which reference other documents. As you know, when you save (or create) a new document mongoose doesn't check the existence of the referenced document. So when I create my APIs I usually write some repeated code (especially if I use express) to validate and check the existence of the reference.

This is something annoying, so I made a very simple plugin called mongoose-idexists that can be used to validate Mongoose Schema paths that are referencing other documents (usually by mongoose ObjectId of the _id field). The module can be used as a full plugin that recursively add a validator to each ref path of any Schema, or it can also be used to protect single schema paths. So you can also protect a subset of you Schema fields.



Simply run:

$ npm install mongoose-idexists --save

Then require it in your project

var idexists = require('mongoose-idexists');  

If you use idexists without any further configuration, it uses the default mongoose connection.

You can add an idexists validator for each Schema paths you want by using idexists.forPath(_the_mongose_path_object)

// require the idexists plugin
var idexists = require('mongoose-idexists');

// Let's define some schemas with ref fields
var personSchema = new Schema({  
    name: String,

var storySchema = new Schema({  
    _creator: {
        type: Schema.Types.ObjectId,
        ref: 'Person'
    fans: [{
        type: Schema.Types.ObjectId,
        ref: 'Person'

// Let's add the validator only to the _creator path

Array of references are supported. So you can use.

// Add the validator also to fans array

You can also recursively add a validator to all the Schema paths (that have references to other documents).

// Let's add the validator to _creator and fans at the same time

// As an alternative you can also use the mongoose plugin notation

// both previous notation produce the same effects of

After the Schema definition as usual you can create the mongoose model. The previous code works for default mongoose connection, so somewhere after configuration you have to use

Story = mongoose.model('Story', storySchema);  
Person = mongoose.model('Person', personSchema);  

Other features

The previous code come from the documentation (README) of the module, and use the default mongoose connection and some other default configuration (for example validation messages). You can see it in the Github repo. Other supported features that you can use are:

  • Custom Connection
  • Custom Validation Messages

How it works

The plugin is very simple, it perform a simple count query (by using TargetModel.count) filtered by the referencing id. The count returning value is checked accordingly to the referencing field type (single object or array). But I really appreciate if you read the code and maybe improve it or give me some suggestions for improvements.

Andrea Tarquini

IT Geek, Full Stack Developer, Programming Language Polyglot and IT security fan.