Difference between revisions of "MongoDB and NodeJs"

(NodeJs-Mongo)
(Errors)
Line 104: Line 104:
 
Solution:
 
Solution:
 
Check your mongo version
 
Check your mongo version
 
+
<source>
 
mongo --version
 
mongo --version
 
If you are using version >= 3.1.0 change you mongo connection file to ->
 
If you are using version >= 3.1.0 change you mongo connection file to ->
Line 113: Line 113:
 
mongoose.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true });
 
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.
 
Ideally, it's a version 4 feature but v3.1.0 and above is supporting it too. Check out MongoDB Github for details.
 +
</source>

Revision as of 15:12, 8 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


Mocha - Unit Test

Mocha is most famous test framework for javascript..


test/create_test.js(test file):

const assert = require('assert');

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

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



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.