Workspace
  • Study Book
  • WEB Network HTTP etc
    • Performance Optimization
    • Performance Optimization
    • HTTP/2 & SPDY
    • WebSocket
    • HTTP Header
    • Cross-Origin Resource Sharing
    • JSON, XML, other format
  • Javascript
    • Promise
    • make API call
    • Web API
    • Common JS
    • AJAX
    • Event DOM and delegation
    • ES6 new features
    • special function
    • function API
  • React
    • class component
    • Example
    • Lifting functions/ state up
    • Hot Loader
    • Testing
    • New Features
    • Hook
    • Simple code
    • Life Cycle
  • CSS
    • Horizontal & Vertical Align
    • GPU Animation
    • transform-function
    • LVHA Pseudo-classes
    • Selector
    • How To CSS
  • HTML
  • Redux
  • NodeJS
    • File System
  • express
    • express questions
    • express mongodb
  • Restful API
  • MongoDB
  • Compare
  • Test
    • Jest
  • Deploy development
  • coding question
  • DevOps
  • Webpack
  • GraphQL
Powered by GitBook
On this page
  • Express and Mongodb - Client
  • mongoose

Was this helpful?

MongoDB

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.

Database

Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection

Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.

Document

A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

Why Use Mongodb

Organizations of all sizes are adopting MongoDB because it enables them to build applications faster, handle highly diverse data types, and manage applications more efficiently at scale.

  • Development is simplified as MongoDB documents map naturally to modern, object-oriented programming languages.

  • MongoDB can be easily scaled within and across multiple distributed data centers.

  • Working with data as flexible JSON documents, rather than as rigid rows and columns, is proven to help developers move faster.

Query Language Comparison

MySQL

INSERT INTO users (user_id, age, status)VALUES ('bcd001', 45, 'A')

SELECT * FROM users

UPDATE users SET status = 'C' WHERE age > 25

MongoDB

db.users.insert({
  user_id: 'bcd001',
  age: 45,
  status: 'A'
});

db.users.find();

db.users.update(
  { age: { $gt: 25 } },
  { $set: { status: 'C' } },
  { multi: true }
)

Express and Mongodb - Client

The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations.

Start the mongo shell

NOTE: Ensure that MongoDB is running before attempting to start the mongo shell.

mongo

Select database

  • To show the list of databases:

show dbs
  • To display the database you are using:

db
  • To switch database:

use <database>

CURD

CRUD is an acronym for Create, Read, Update and Delete, which are 4 types of operations in MongoDB.

  • Create - add new documents to a collection

  • Read - retrieve documents from a collection

  • Update - modify existing documents in a collection

  • Remove - remove documents from a collection

Create Operations

Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.

MongoDB provides the following methods to insert documents into a collection:

  • db.collection.insertOne()

  • db.collection.insertMany()

For example:

Read Operation

Read operations retrieves documents from a collection; i.e. queries a collection for documents. MongoDB provides the following methods to read documents from a collection:

  • db.collection.find()

Find all documents...

You can specify query filters or criteria that identify the documents to return. For exmaple,

find({object}) ----- object : restriction

  • db.users.find() will give you all documents in the users collection.

  • db.users.find({status: "pending"}) will only give you the documents with status="pending"

Update Operation

Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection:

  • db.collection.updateOne()

  • db.collection.updateMany()

  • db.collection.replaceOne()

db.users.updateOne({age: 26}, {$set: {status: 'reject'}});

Delete Operation

Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:

  • db.collection.deleteOne()

  • db.collection.deleteMany()

  • db.collection.remove() ---- one or multiple. second parameter: true only one.

  • remove must has a parameter... or .... delete everything.

db.users.deleteOne({age: 26}});

mongoose

Schemas

Mongoose defines a schema for your data models so your documents follow a specific structure with pre-defined data types.

Validation

Mongoose has built in validation for schema definitions. This saves you from writing a bunch of validation code that you have to otherwise write with the MongoDB dirver.

Instance Methods

Mongoose provides optional pre and post save operations for data models. This makes it easy to define hooks and custom functionality on successful reads / writes.

Returning results

Mvongoose makes returning updates documents or query results easier

PreviousRestful APINextCompare

Last updated 5 years ago

Was this helpful?

Example
Example
Example
Example