Difference between revisions of "MongoDB and NodeJs"

(Automating Test=)
(Search/Find Record on Mongo)
Line 96: Line 96:
  
 
===Search/Find Record on Mongo===
 
===Search/Find Record on Mongo===
 +
'''reading_test.js''':
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
const assert = require('assert');
 
const assert = require('assert');
Line 120: Line 121:
 
               done();
 
               done();
 
           });
 
           });
 +
    });
 +
 +
    it('find a user with a particular id', (done)=>{
 +
        User.findOne({_id:joe._id})
 +
        .then((user)=>{
 +
            assert(user.name === 'Joe');
 +
            done();
 +
        });
 
     });
 
     });
 
});
 
});

Revision as of 10:29, 10 November 2018

MongoDB

NodeJs

Installation

We will install :

  • MongoDb
  • Node
  • Robomongo - editor

brew - package manager can be used on MacOs

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

https://brew.sh/

Mongo Installation:

>brew install mongo
>mongod
>sudo mkdir -p /data/db
>sudo chown -Rv rasimsen /data/db
>brew services start mongo #start/stop/status

NodeJs-Mongo

$ mkdir users => cd users
$ mkdir src
$ mkdir test
$ npm install request --save #url için
$ npm install --save mocha nodemon mongoose

mocha: unit test nodemon: mongoose : mongo db driver

src/user.js:

const mongoose=require('mongoose');
const Schema=mongoose.Schema;

const UserSchema = new Schema({
    name:String
});

const User = mongoose.model('User',UserSchema);

module.exports = User;


Mocha - Unit Test

Mocha is most famous test framework for javascript..

Creating data on MongoDb/Test

test/create_test.js(test file):

const assert = require('assert');

describe('Creating records',()=>{
    it('saves a user',()=>{
        assert(1+1 ===2);
    });
});

test/create_test.js(another sample):

const assert = require('assert');
const User = require('../src/user');


describe('Creating records',()=>{
    it('saves a user',(done)=>{ //<------------ 1.callback 
        const joe = new User({ name: 'Joe'});
        
        //joe.save();
        joe.save()
        .then((result) => {
            //Has joe been saved successfully?
            assert(!joe.isNew);
            done(); //<------------------- 2.callback: we should use 
        }).catch((err) => {
            console.log('Error');
        });

    });
});

Search/Find Record on Mongo

reading_test.js:

const assert = require('assert');
const User = require('../src/user');

describe('Reading users out of the database', ()=>{
    let joe;

    beforeEach((done)=>{
        joe=new User({name:'Joe'});
        joe.save()
        .then((result) => {
            done(); 
        }).catch((err) => {
            console.log('Error..');
        });
    });
    
    it('finds all users with a name of joe',(done)=>{
        User.find({ name:'Joe' })
           .then((users)=>{
               console.log(users);
               assert(users[0]._id.toString() === joe._id.toString());
               done();
           });
    });

    it('find a user with a particular id', (done)=>{
        User.findOne({_id:joe._id})
        .then((user)=>{
            assert(user.name === 'Joe');
            done();
        });
    });
});


package.json file:

{
  "name": "users",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mocha": "^5.2.0",
    "mongoose": "^5.3.10",
    "nodemon": "^1.18.6",
    "request": "^2.88.0"
  }
}


$ npm run test

Automating Test

   "test": "nodemon --exec 'mocha -R min'" : formats and clear the output

If I change the code, it runs test again for changes.

{
  "name": "users",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "nodemon --exec 'mocha -R min'"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mocha": "^5.2.0",
    "mongoose": "^5.3.10",
    "nodemon": "^1.18.6",
    "request": "^2.88.0"
  }
}



Errors

(node:27919) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

Solution: Check your mongo version

mongo --version
If you are using version >= 3.1.0 change you mongo connection file to ->

MongoClient.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true })
or your mongoose connection file to ->

mongoose.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true });
Ideally, it's a version 4 feature but v3.1.0 and above is supporting it too. Check out MongoDB Github for details.

Error: TypeError: User is not a constructor Solution: Node is a case-sensetive language. Check your model or object..