mongoDB
Robo 3T : MongoDB GUI 工具
download RoBo 3T (for windows .zip version 即可)
![pic8](/2021/11/06/db-3/pic8.png)
![pic9](/2021/11/06/db-3/pic9.png)
解壓縮後執行 robo3t.exe
![pic1](/2021/11/06/db-3/pic1.png)
選擇 I agree , Next
![pic2](/2021/11/06/db-3/pic2.png)
選擇 Finish
![pic3](/2021/11/06/db-3/pic3.png)
選擇 Create, 填入由MongoDB atlas 取得 url(Node.js 4.0 or later 版本即可), 選 Form URI
![pic4](/2021/11/06/db-3/pic4.png)
選擇 Save
![pic5](/2021/11/06/db-3/pic5.png)
選擇 Connect 即可見到 DB 畫面
![pic6](/2021/11/06/db-3/pic6.png)
![pic7](/2021/11/06/db-3/pic7.png)
MongoDB Altas add project
選擇 New Project, Next, Create Project
![pic11](/2021/11/06/db-3/pic11.png)
![pic12](/2021/11/06/db-3/pic12.png)
![pic13](/2021/11/06/db-3/pic13.png)
Databases : Build a Database, Shared(Create), select position+Create Cluster
![pic14](/2021/11/06/db-3/pic14.png)
![pic15](/2021/11/06/db-3/pic15.png)
![pic16](/2021/11/06/db-3/pic16.png)
![pic17](/2021/11/06/db-3/pic17.png)
![pic18](/2021/11/06/db-3/pic18.png)
Network Access : Add IP Address, 0.0.0.0/0(表不限制), Confirm
![pic19](/2021/11/06/db-3/pic19.png)
![pic20](/2021/11/06/db-3/pic20.png)
![pic21](/2021/11/06/db-3/pic21.png)
Database Access: add New DataBase User, set user info, add User
![pic22](/2021/11/06/db-3/pic22.png)
![pic23](/2021/11/06/db-3/pic23.png)
![pic24](/2021/11/06/db-3/pic24.png)
get link url, Databases: Connect, Connect your application,
![pic25](/2021/11/06/db-3/pic25.png)
![pic26](/2021/11/06/db-3/pic26.png)
NOde.js 4.0 or later driver
使用時要填入正確的 password
myFirstDataBase 為 default DB name, 可改為其他名稱
![pic27](/2021/11/06/db-3/pic27.png)
NOde.js 2.2.12 or later driver(某些applicat要使用這個版本才能使用)
使用時要填入正確的 password
myFirstDataBase 為 default DB name, 可改為其他名稱
![pic28](/2021/11/06/db-3/pic28.png)
加入 database, Databases : add My Own data, database info+Create
![pic29](/2021/11/06/db-3/pic29.png)
![pic30](/2021/11/06/db-3/pic30.png)
![pic31](/2021/11/06/db-3/pic31.png)
MongoDB
Schema
type
- String
- Number
- Array
- ObjectId
- Boolean
special
map to other
1
2
3
4
5category: {
type: ObjectId,
ref: "Category",
required: true,
},photo
1
2
3
4photo: {
data: Buffer,
contentType: String,
},simple string
1
salt: String,
default
- 0
- []
othert field
- trim : true
- required: true
- maxlength: 32
- unique: 32
- timestamps: true
$regex
1 | const query = {}; |
control
- Category.find().exec((err, data) => {}) : find all
- Product.find({ _id: { $ne: req.product }, category: req.product.category }): find by consition, $ne: not include
- Product.find(query, (err, products) => {}) : find direct run, not run exec
- findById(id).exec((err, product) => {} ) : find by id
- select(“-photo”) : remove field
- populate(“category”) : link to another table
- sort([[sortBy, order]]) : sort
- skip(skip) : set skip
- limit(limit) : set limit
- remove((err, deletedProduct) => {} ) : remove
- findOneAndUpdate() : find + update
1
2
3
4
5.findOneAndUpdate(
{ _id: req.profile._id },
{ $set: req.body },
{ new: true },
(err, user) => {})
mongoose : mongoDB object modeling tool to work in an asynchronous environment. Mongoose supports both promises and callbacks.
example code
1 | // connect mangoDB altas |
Deprecation Warnings
update()
1 | // Replace this: |
remove()
1 | // Replace this: |
count()
1 | // Replace this: |