CRUD MongoDB with Node.js
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.
Contents
- Create Schema & Insert one in Document
- Select data In Document
- Update data In Document
- Delete data In Document
1. Create Schema & Insert one in Document
Check the result in your https://cloud.mongodb.com/
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
$eq
Matches values that are equal to a specified value.
$gt
Matches values that are greater than a specified value.
$gte
Matches values that are greater than or equal to a specified value.
$in
Matches any of the values specified in an array.
$lt
Matches values that are less than a specified value.
$lte
Matches values that are less than or equal to a specified value.
$ne
Matches all values that are not equal to a specified value.
$nin
Matches 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 จีราวุธ วารินทร์