๐8์ฅ ํค์๋๐
Nosql vs. SQL
MongoDB
mongoose
RDB์์ ์ฌ์ฉํ๋ SQL ์ธ์ ์ธ์ด๋ฅผ ์ฌ์ฉํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ NoSQL์ด๋ผ๊ณ ํ๋ค.
์๋ ํ๋
MongoDB Community Server ๋ค์ด๋ก๋ https://www.mongodb.com/try/download/community
Try MongoDB Community Edition
Try MongoDB Community Edition on premise non-relational database including the Community Server and Community Kubernetes Operator for your next big project!
www.mongodb.com
MongoDB ์
ธ ๋ค์ด๋ก๋ https://www.mongodb.com/try/download/shell
์ค์น๊ฒฝ๋ก C:\Users\USER\AppData\Local\Programs\mongosh\
Try MongoDB Tools - Download Free Here
Free download for MongoDB tools to do more with your database. MongoDB Shell, Compass, CLI for Cloud, BI Connector and other database tools available.
www.mongodb.com
๋ชฝ๊ณ ๋๋น ํฌํธ๋ 27017๋ฒ
๋ชฝ๊ณ ๋๋น์ '์ปฌ๋ ์ ' = mysql์ 'ํ ์ด๋ธ'
MySQL์ ์ํ๋ผ์ด์ฆ๊ฐ ์๋ค๋ฉด, ๋ชฝ๊ณ ๋๋น์๋ ๋ชฝ๊ตฌ์ค(mongoose)๊ฐ ์๋ค
๋ชฝ๊ตฌ์ค ํจํค์ง ์ค์น npm i mongoose
๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ ์ฃผ์ ํ์: mongodb://[username:password@]localhost[:port][/[database][?options]]
์: mongodb://YUZ:0000@localhost:27017/admin
// schemas/index.js
const mongoose = require('mongoose');
const connect = () => {
if (process.env.NODE_ENV !== 'production') {
mongoose.set('debug', true);
}
mongoose.connect('mongodb://YUZ:0000@localhost:27017/admin', {
dbName: 'nodejs',
useNewUrlParser: true,
}).then(() => {
console.log("๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ ์ฑ๊ณต");
}).catch((err) => {
console.error("๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ ์๋ฌ", err);
});
};
mongoose.connection.on('error', (error) => {
console.error('๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ ์๋ฌ', error);
});
mongoose.connection.on('disconnected', () => {
console.error('๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ์ด ๋๊ฒผ์ต๋๋ค. ์ฐ๊ฒฐ์ ์ฌ์๋ํฉ๋๋ค.');
connect();
});
module.exports = connect;
// schemas/user.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
name: {
type: String,
required: true,
unique: true,
},
age: {
type: Number,
required: true,
},
married: {
type: Boolean,
required: true,
},
comment: String,
createdAt: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model('User', userSchema);
// schemas/comment.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
const { Types: { ObjectId } } = Schema;
const commentSchema = new Schema({
commenter: {
type: ObjectId,
required: true,
ref: 'User',
},
comment: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model('Comment', commentSchema);
๋น์นธ ์ฑ์ฐ๊ธฐ ๋ฌธ์ (๋น์นธ์ ๋๋๊ทธํด์ ์ ๋ต์ ๋งํ ๋ณด์ธ์!)
1. MySQL์ ํ ์ด๋ธ, ๋ก์ฐ, ์ปฌ๋ผ์ ๋ชฝ๊ณ ๋๋น์์๋ ๊ฐ๊ฐ (์ปฌ๋ ์ , ๋คํ๋จผํธ, ํ๋)๋ผ๊ณ ๋ถ๋ฅธ๋ค.
2. ๋ชฝ๊ณ ๋๋น์๋ (์คํค๋ง)๊ฐ ์์ด ํ๋ ๋๋ฉ์ธ ๊ด๋ฆฌ์ ์ ์ํด์ผ ํ๋ค. ๋ชฝ๊ตฌ์ค์๋ ๊ทธ๊ฒ์ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๋ ๊ธฐ๋ฅ์ด ์๋ค.
3. ์ํ๋ผ์ด์ฆ๋ ORM(Object Relational Mapping), ๋ชฝ๊ตฌ์ค๋ (ODM, Object Document Mapping)์ด๋ค.
4. ๋ชฝ๊ตฌ์ค ๋คํ๋จผํธ CRUD ๋ฉ์๋ ์ด๋ฆ: ์์ฑ(insertOne), ์กฐํ(find, findOne. findMany), ์์ (updateOne, updateMany), ์ญ์ (deleteOne, deleteMany)
5. ์กฐํ ์กฐ๊ฑด์ ์ค์ ํ ๋ ์ฐ์ฐ์ ์์ ๋ถ๋ ๊ธฐํธ๋ ($)์ด๋ค. (gt, gte, ne, or ๋ฑ)
6. ํน์ ๋คํ๋จผํธ์ ํ๋ ํ๋๋ฅผ ์์ ํ ๋: updateOne({๊ฒ์์กฐ๊ฑด}, {$set: {์์ ํ ํ๋: ๊ฐ}})
[๋ ธ๋ 2] 10์ฅ. ์น API ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.12.22 |
---|---|
[๋ ธ๋ 2] 9์ฅ. ์ต์คํ๋ ์ค๋ก SNS ์๋น์ค ๋ง๋ค๊ธฐ (1) | 2023.12.01 |
[๋ ธ๋ 2] 7์ฅ. MySQL (0) | 2023.11.17 |
[๋ ธ๋ 2] 6์ฅ. ์ต์คํ๋ ์ค ์น ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.11.10 |
[๋ ธ๋ 2] 5์ฅ. ํจํค์ง ๋งค๋์ (0) | 2023.11.03 |