Set composite key in mongodb through mongoose using Node.js

In this post we will see how to maintain unique value in mongoDb in our Node.js project. Using unique key property of mongoose schema we can avoid column level duplicate value. For example consider Role collection have role name and description column. Here role name should not repeated. In SQL using unique constrain we can do that. Let see how to do it in monode.js project.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var roleSchema = new Schema({
name: {type:String, required:true,unique:required}
description: {type:String}
});
var model = mongoose.model(role, roleSchema);
To save record in mongodb roles collection
var roleData =[
{“name”:”Admin”,” description”:”Admin role”},
{“name”:”Employee”,” description”:”Employee role”}];
roleData.forEach(function(data){
model.insert(data, function (err, resul) {
console.log(resul._Id);
});
});
Now let see how to maintain unique value in collection based on combination of more than one column.Let conceder multi-tenant application. In this use case each tenant have separate roles. Roles may or may not same in both tenant.
Let see the example.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var roleSchema = new Schema({
name: {type:String, required:true },
tenant : {type:String, required:true },
description: {type:String}
});
roleSchema.index({name: 1, tenant: 1}, { unique: true });
var model = mongoose.model(role, roleSchema);
To save record in mongodb roles collection
var roleData =[ {“name”:”Admin”,” description”:”Admin role”,”tenant”:”tenant1”},
{“name”:”Employee”,” description”:”Employee role”,”tenant”:”tenant1”},
{“name”:”Admin”,” description”:”Admin role”,”tenant”:”tenant2”},
{“name”:”Employee”,” description”:”Employee role”,”tenant”:”tenant2”}];
roleData.forEach(function(data){
model.insert(data, function (err, resul) {
console.log(resul._Id);
});
});

Grunt Tutorial

What is grunt

In one word: automation. The less work you have to do when performing repetitive tasks like minification, compilation, unit testing, linting, etc, the easier your job becomes. After you've configured it through a Grunt file, a task runner can do most of that mundane work for you—and your team—with basically zero effort.

Grunt and Grunt plugins are installed and managed via npm, the Node.js package manager.

Install Grunt

For windows machine open the cmmand windows in Administrator permission and run npm install -g grunt-cli command.

For Linus machine oen terminal and run sudo npm install -g grunt-cli .

How to use Grunt

Now we can see how to use grunt in our project. In our project we have to do lot of thing such that minification, compilation, unit testing etc. let see how to use one simple thing run using grunt

Crate a folder as “GruntTutorial” using following command mkdir GruntTutorial.
Move to GruntTutorial folder using cd GruntTutorial.
Just run following command.
grunt
It show following error Fatal error: Unable to find Gruntfile Now let see how to solve the error
Run the following command npm install grunt
Create a java script file and name it as Gruntfile.js and save it in “GruntTutorial”. Run the following command
grunt
It show the warning The task default not found. Use --force to continue.
Now see how to create simple grunt task
Open the Gruntfile.js in any one editor and type following code and save it.

module.exports = function(grunt){
grunt.registerTask("default",function(){
grunt.log.writeln("Hello World");
})
}


Run the following command
grunt
You got following result
Running "default" task
Hello World
Done, without errors.