My Notebook

My knowledge sharing platform

What Is Schema and Model in Mongoose?

| Comments

Hi Friends,

Today we will discuss what is schema and model in mongoose and what is the real purpose behind these concept.

So let’s start. Before going to topic we will discuss about some concept first.

CONCEPT

1) What is mongoose and why it needed ?

Ans: In real world , our server side code(may be java, python, node etc) need to interact with database.For this blog, we will stick with Node and mongoDB. MongoDB stores data in document manner.

We have object in our node.js code which we need to store in MongoDB. So we need something which can map our object to document so that we can store it in database.That mapper is known as “object document mapper” or ODM.

So for Node.js and mongodb we have ODM known as “mongoose”.

Now we are clear about what is mongoose and why it is needed now we will dive into our topic .So we need to understand how ‘Mongoose’ doing this mapping.

Let’s consider our MongoDB structure.

people               -- > collection name
-------------
{
  id:'1',                ----> document-1
  name :'Bob'
}
{
  id:'2',                ----> document-2
  name :'John'
}   

so now we need one blueprint/constructor which can represent common structure of each document present in collection. This blueprint/constructor is known as “Model”. So every object created from this blueprint will be treated as document in mongoDB.Now our server side code will interact with mongoDB document via this “Model”.

Here if we observe we can say collection consist of a number of Model.

so Mongoose automatically looks for the plural version of your model name for collection name.

Example: 
modelName                   Collection Name
---------                   ---------------
Person                       People
task                         tasks

Now our Model need to represent common structure. We define that structure in a object known as ‘Schema’. So we need to derive our model from ‘Schema’. So in mongoose documentation we can see the below line

Models are fancy constructors compiled from Schema definitions. An instance of a model is called a document. Models are responsible for creating and reading documents from the underlying MongoDB database.

I hope you understood the concepts of schema and model in mongoose.

You can share your thoughts or doubts in comment section .

'This' Keyword Under the Hood in Javascript

| Comments

Hi Friends, In this post we will discuss about ‘this’ keyword in javascript and how it works in javascript. I will try to make this post as simple as possible. let’s jump into it .

concept-1

When we are declaring a variable or function in javascript it became a property of window object . In the global excution context ‘this’ keyword always refer to ‘window’ object. so example:

    var a = 'javascript';
    console.log(a) // javascript
    console.log(window.a) // javascript
    console.log(this.a) // javascript
    function xyz(){
        console.log(this.a) 
    }
    xyz() // javascript

concept-2

“this” keyword inside a method always refers to calling object .Example:

    var obj = {
            name:'javascript',
            print : function(){
                console.log(this.name)
            }
        }
        obj.print(); // javascript

Here object ‘obj’ is calling ‘print’ method. so ‘this’ keyword inside print will refer to object ‘obj’ .

concept-3

When we are using a inner function inside a method then ‘this’ keyword inside that inner function refers to ‘window’ object, not calling object.

Example:

    var name = 'window';
    var obj = {
            name:'javascript',
            print : function(){
                return function(){
                    console.log(this.name)
                }
            }
        }
        obj.print()(); // window 

Then how to fix this .

We have to assign ‘this’ object to some other object inside print function.Now

    var name = 'window';
    var obj = {
            name:'javascript',
            print : function(){
                var self = this // here we assigned "this" object to self object.
                return function(){
                    console.log(self.name)
                }
            }
        }
        obj.print()(); // javascript

I hope these three concepts are clear now.

Happy Coding :)

Windows and Octopress Mystery

| Comments

Hi Folks,

Octopress is one of the favourite tool for those( specially for Programmers) who loves to write and post something in a blog . If you are using linux system , then octopress installation and setup will be easier than those who are using windows. I am using windows8 and tried to setup octopress in my machine. Believe me , it took nearly 2 days to setup octopress because of some errors which I am going to explain in this Article. So let’s start.

If you want to install octopress then you can go through this Article. After completing every step in above article ,When I run the command bundle install , I got the following error

Fetching gem metadata from https://rubygems.org/
Fetching version metadata from https://rubygems.org/
Fetching dependency metadata from https://rubygems.org/
Resolving dependencies...
Using rake 10.5.0
Using RedCloth 4.2.9
Using addressable 2.4.0
Using blankslate 2.1.2.4
Using chunky_png 1.3.6
Installing fast-stemmer 1.0.2 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    C:/Ruby193/bin/ruby.exe extconf.rb
Invalid argument - C:/Ruby193/bin/ruby.exe extconf.rb 2>&1

Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/fast-stem
mer-1.0.2 for inspection.
Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/extensions/x86-mingw32/1.9.1/fa
st-stemmer-1.0.2/gem_make.out
Using coffee-script-source 1.10.0
Using execjs 2.7.0
Using colorator 0.1
Using multi_json 1.12.1
Using sass 3.4.22
Using rb-fsevent 0.9.7
Using ffi 1.9.14
Using multipart-post 2.0.0
Using tilt 2.0.5
Using jekyll-paginate 1.1.0
Using kramdown 1.12.0
Using liquid 2.6.3
Using mercenary 0.3.6
Installing posix-spawn 0.3.11 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    C:/Ruby193/bin/ruby.exe extconf.rb
Invalid argument - C:/Ruby193/bin/ruby.exe extconf.rb 2>&1

Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/posix-spa
wn-0.3.11 for inspection.
Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/extensions/x86-mingw32/1.9.1/po
six-spawn-0.3.11/gem_make.out
Installing yajl-ruby 1.2.1 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    C:/Ruby193/bin/ruby.exe extconf.rb
Invalid argument - C:/Ruby193/bin/ruby.exe extconf.rb 2>&1

Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/yajl-ruby
-1.2.1 for inspection.
Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/extensions/x86-mingw32/1.9.1/ya
jl-ruby-1.2.1/gem_make.out
Installing redcarpet 3.3.4 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    C:/Ruby193/bin/ruby.exe extconf.rb
Invalid argument - C:/Ruby193/bin/ruby.exe extconf.rb 2>&1

Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/redcarpet
-3.3.4 for inspection.
Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/extensions/x86-mingw32/1.9.1/re
dcarpet-3.3.4/gem_make.out
Using safe_yaml 1.0.4
Using rack 1.6.4
Installing rdiscount 2.2.0.1 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    C:/Ruby193/bin/ruby.exe extconf.rb
Invalid argument - C:/Ruby193/bin/ruby.exe extconf.rb 2>&1

Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/rdiscount
-2.2.0.1 for inspection.
Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/extensions/x86-mingw32/1.9.1/rd
iscount-2.2.0.1/gem_make.out
Using stringex 1.4.0
Using bundler 1.12.5
Installing jekyll-sitemap 0.11.0

Errno::EINVAL: Invalid argument - C:/Ruby193/lib/ruby/gems/1.9.1/gems/jekyll-sit
emap-0.11.0/spec/fixtures/_posts/2016-04-01-i¶↓h
Using parslet 1.5.0
An error occurred while installing fast-stemmer (1.0.2), and Bundler cannot
continue.
Make sure that `gem install fast-stemmer -v '1.0.2'` succeeds before bundling.

If you are finding this error then try to run the following command SET COMSPEC and see the result . The output should be like

ComSpec=C:\Windows\system32\cmd.exe

If you find any other result other than above (In my case I got ComSpec=C:\Windows\system32\cmd.exe;C:\Ruby21-x64) then change it to ComSpec=C:\Windows\system32\cmd.exe by changing the ComSpec variable .

How to change ComSpec in windows.

  1. Right click on Computer
  2. Goto Properties -> Advanced System Settings
  3. Click on Environment Variables
  4. Goto System Variables -> ComSpec
  5. Click on Edit and remove the Extra text(in my case it was C:\Ruby21-x64) and don’t forget to remove “;”
  6. After Edit it should be like ComSpec=C:\Windows\system32\cmd.exe
  7. Click on OK

Now run the command bundle install .If you are getting the some error like

'make' is not recognized as an internal or external command, operable program or batch file.

It may be due to the following reason.

The Ruby Development Kit has a component called MinGW which is used to run Unix command on Windows and Ruby Development Kit not added to the system path variable

So try the following solution to resolve the problem.

  1. Goto your devKit location and run the following command in terminal
  2. ruby dk.rb init
  3. ruby dk.rb install
  4. devkitvars.bat –> This command I had missed while installation

Now setup octopress and start your blog.

Happy Coding. :)

Some reference link:

https://github.com/imathis/octopress/wiki/Installation-Instructions-2.0-on-Windows https://github.com/oneclick/rubyinstaller/wiki/Troubleshooting http://stackoverflow.com/questions/27239491/cannot-install-gem-make-is-not-recognized-as-an-internal-or-external-command-o/33442847#33442847

A Funny App

| Comments

Hello folks,

After making of two games ,I have completely fallen in love with canvas and javascript .So i had decided to make another game by using this.This time i thought to make a funny simple game. So i have decided to make a game name as “KNOW YOUR STRENGTH” .Actually In this game a button is attached with a meter containing a ball inside it. You have to hit that button with a hammer .As u hit that button ,the ball inside the meter grows up.The meter is divided into 4 part named as “WEAK”,”AVERAGE”,”STRONG”,”SUPER STRONG”.The ball inside the meter stops in a definite part and according to that part the meter gives your strength. So simple game.

Air-escape 2

| Comments

Hi Viewers,

Before going to describe about the second version of my game let me describe a small intro about my first game. Actually my first game is a small helicopter game in which u have to overcome various obstacles to reach the destination to win it (Not a great game at all).When i started learning javascript and jquery ,then i decided to create that game .It is just simple game without canvas.

some drawbacks of my first game:

1)Obstacles are static

2)no level (means difficulty level)

3)No score and many more

To Avoid such drawbacks ,i started learning HTML canvas and more on javascript so that i can rectify my errors in the next game.Then I created the second version of that game by using canvas to make it a beautiful one. Then after working for 2 days ,i successfully created the second version of that game by adding some more features in it.Those are:

1)moving objects(obstacles)

2)Three Difficulty level(easy,medium and hard)

3)Score board

4)more attractive background than previous one

5)Reset Button to play again..