CRUD MongoDB with Node.js

patchaya sonta
3 min readOct 13, 2020

Hello, everyone my name’s Knot I'm a developer in Thailand. On rainy days I’m talking about CRUD in MongoDB with Node.js.

Example ER Diagram for this CRUD

Contents

  1. Create Schema & Insert one in Document
  2. Select data In Document
  3. Update data In Document
  4. Delete data In Document

1. Create Schema & Insert one in Document

create collection and insert data

Check the result in your https://cloud.mongodb.com/

result in your mongo account

2. Select data in Document

Select all data in Document in employee

Result in terminal

Select data with condition

Ex. select of employee where gender is ‘F’

Result in terminal

select limit and sort

Ex. select gender is ‘F’ limit 3 data and sort by age DESC of employee

  • sort 1 = Ascending
  • sort -1 = Descending

select data with some property

Ex. select Id, name, gender of employee

If you don’t show id, name, gender passing value = 0 instances 1

Comparison Query Operators

$eqMatches values that are equal to a specified value.

$gtMatches values that are greater than a specified value.

$gteMatches values that are greater than or equal to a specified value.

$inMatches any of the values specified in an array.

$ltMatches values that are less than a specified value.

$lteMatches values that are less than or equal to a specified value.

$neMatches all values that are not equal to a specified value.

$ninMatches none of the values specified in an array.

select data with comparison query operators

Ex. select employee age is more than 20 and gender is ‘F’

Count all in employees Document

3. Update

update one in document

Ex. update depId of employee name ‘Dynac’ is ‘D03’

4. Delete in Document

Ex. delete id ‘E01’ of employees

Thank you :)

REF

Book : พัฒนาเว็บเเอพลิเคชันด้วย Node.js Express+MongoDB จีราวุธ วารินทร์

Another content

Begin Node.js with Mongo DB

--

--