Giter Club home page Giter Club logo

mongodb-guide's Introduction

MongoDB Node.js Quick Start Guide

Quick start guide for installing and configuring MongoDB and using the Node.js mongoose library.

Install MongoDB on Windows

Download Center

Simplify the path in the installer, I used C:\Program Files\MongoDB

Initial Setup

Create the following dirs:

  • C:\Program Files\MongoDB\data\db - to house the databases
  • C:\Program Files\MongoDB\log - to house log files

Create a config file:

  • C:\Program Files\MongoDB\mongo.cfg

To install as a service

mongod.exe --directoryperdb --dbpath "C:\Program Files\MongoDB\data\db" --logpath "C:\Program Files\MongoDB\log\mongo.log" --logappend --config "C:\Program Files\MongoDB\mongo.cfg" --rest --install

Enable Authentication

Enabling Authentication

To enable authorization, connect to server and add user to admin database

use admin
db.createUser({
    user: <username>,
    pwd: <plaintext password>,
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    });

More Info on MongoDB Roles

In mongo.cfg (YAML), add the following:

security:
    authorization: enabled

Restart the MongoDB service

net stop MongoDB
net start MongoDB

Config File Options

Using Mongoose

Mongoose Docs

Installation

npm install --save mongoose

Connecting

const mongoose = require('mongoose');

// Connect to db

// Pattern
//mongoose.connect('mongodb://<username>:<password>@host/<dbname>', {<options>});

// Example
mongoose.connect('mongodb://brycejech:myPassword@localhost/test', {useMongoClient: true, keepAlive: 1});
// Note that special chars in username/password should be URL percent-encoded

Schema

With Mongoose, everything is derived from a Schema. Schemas map to a collection and defines what documents in that collection look like. Each key in a Schema defines a property that will be cast to it's associated SchemaType.

SchemaTypes:

  • String
  • Number
  • Date
  • Buffer
  • Boolean
  • Mixed
  • ObjectId
  • Array

Example:

const Schema = mongoose.Schema;

let animalSchema = new Schema({
    // SchemaTypes that are not standard js types seem to be in Schema.Types
    id:         Schema.Types.ObjectId,
    name:       String,
    species:    String,
    actions:    [String], // Array of strings
    numLegs:    Number,
    isAlive:    Boolean,
    born:       Date
});

Note: In order to specify a collection when defining a schema, you must pass in an optional options object as the second argument with the collection name to the Schema constructor.

Example:

let animalSchema = new Schema({<schema object>}, {collection: 'animals'});

Alternatively, if your collection name is plural, i.e. 'animals', then you may indicate your collection name as the singular form of the word when converting your Schema to a Model, more on that later.

Models

To use the Schema definition, we have to convert our Schema into a Model. Pass a model name and the schema to mongoose.model(modelName, schema);.

Example:

let animal = mongoose.model('animal', animalSchema);

Note: Mongoose will pluralize the model name and use that as the collection name unless you pass in the name when defining your schema like this: let animalSchema = new Schema({...}, {collection: 'animals'});. For example, if your collection name is animals, then you would create your model like this: let animal = mongoose.model('animal', animalSchema);, and mongoose will use 'animal' + 's', or animals as the collection name.

Instance of Models ARE documents.

mongodb-guide's People

Contributors

brycejech avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.