mongoDB

Robo 3T : MongoDB GUI 工具

download RoBo 3T (for windows .zip version 即可)

解壓縮後執行 robo3t.exe

選擇 I agree , Next

選擇 Finish

選擇 Create, 填入由MongoDB atlas 取得 url(Node.js 4.0 or later 版本即可), 選 Form URI

選擇 Save

選擇 Connect 即可見到 DB 畫面

MongoDB Altas add project

選擇 New Project, Next, Create Project

Databases : Build a Database, Shared(Create), select position+Create Cluster

Network Access : Add IP Address, 0.0.0.0/0(表不限制), Confirm

Database Access: add New DataBase User, set user info, add User

NOde.js 4.0 or later driver

使用時要填入正確的 password
myFirstDataBase 為 default DB name, 可改為其他名稱

NOde.js 2.2.12 or later driver(某些applicat要使用這個版本才能使用)

使用時要填入正確的 password
myFirstDataBase 為 default DB name, 可改為其他名稱

加入 database, Databases : add My Own data, database info+Create

MongoDB

Schema

type
  • String
  • Number
  • Array
  • ObjectId
  • Boolean
special
  • map to other

    1
    2
    3
    4
    5
    category: {
    type: ObjectId,
    ref: "Category",
    required: true,
    },
  • photo

    1
    2
    3
    4
    photo: {
    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
2
3
4
5
const query = {};
query.name = { $regex: req.query.search, $options: "i" };
Product.find(query, (err, products) => {
......
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// connect mangoDB altas
// using 2.2.12 or later's uri
const mongoose = require("mongoose");
var uri =
"mongodb://robert2:{password}@cluster0-shard-00-00.bscvu.mongodb.net:27017,cluster0-shard-00-01.bscvu.mongodb.net:27017,cluster0-shard-00-02.bscvu.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-11cyyj-shard-0&authSource=admin&retryWrites=true&w=majority";
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("MongoDB Connected…");
})
.catch((err) => console.log(err));

// save data
const User = mongoose.model("User", userSchema);
new User(req.body).save;

// get data
User.findOne({ email })

Deprecation Warnings

update()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Replace this:
MyModel.update({ foo: 'bar' }, { answer: 42 });
// With this:
MyModel.updateOne({ foo: 'bar' }, { answer: 42 });

// If you use `overwrite: true`, you should use `replaceOne()` instead:
MyModel.update(filter, update, { overwrite: true });
// Replace with this:
MyModel.replaceOne(filter, update);

// If you use `multi: true`, you should use `updateMany()` instead:
MyModel.update(filter, update, { multi: true });
// Replace with this:
MyModel.updateMany(filter, update);
remove()
1
2
3
4
5
6
7
8
9
// Replace this:
MyModel.remove({ foo: 'bar' });
// With this:
MyModel.deleteMany({ foo: 'bar' });

// Replace this:
MyModel.remove({ answer: 42 }, { single: true });
// With this:
MyModel.deleteOne({ answer: 42 });
count()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Replace this:
MyModel.count({ answer: 42 });
// With this:
MyModel.countDocuments({ answer: 42 });

// If you're counting all documents in the collection, use
// `estimatedDocumentCount()` instead.
MyModel.count();
// Replace with:
MyModel.estimatedDocumentCount();

// Replace this:
MyModel.find({ answer: 42 }).count().exec();
// With this:
MyModel.find({ answer: 42 }).countDocuments().exec();

// Replace this:
MyModel.find().count().exec();
// With this, since there's no filter
MyModel.find().estimatedDocumentCount().exec();

參考資料