π§ μν©
κ°μΈκ³Όμ κ° μμλμλ€.
expressλ₯Ό μ¬μ©ν΄μ crud κΈ°λ₯μ ꡬννλ 건λ°,
mongoDB ν΄λΌμ°λλ‘ μ°κ²°νλ λ°©λ²μ μ¬μ©ν΄μΌ νλ€.
μ°κ²° νμ μ€μ κ±°κΈ°μ λκ° μμ ν΄μ μ°κ²°ν΄μΌ νλλ°,
μ°κ²°λμλ€λ μ½μμ μ°νμ§λ§, λ°μ΄ν°κ° λ€μ΄κ°κ³ λμ€κ³ μ΄λ° μμ λ€μ΄ μ§νλμ§ μμλ€.
λΆλͺ 곡μλ¬Έμμλ, λ€λ₯Έ κΈλ€μλ db.collection().insertOne() λ μ΄λ°μμΌλ‘ μ¬μ©νλΌκ³ λμμλλ°...
κ³μ dbλ₯Ό μ°Ύμμ μλ€λλ, μ μλμ§ μμλ€λλ....
γ γ γ γ
λλ체 μ μ΄λ¬λ κ±΄μ§ μ΄ν΄ν μ μμλ€...πΉ
ESM λ°©μμ΄λ commonjs λ°©μμΌλ‘ λλμ΄μ Έ μκ³ ,
ν΄λΌμ°λ νμμ΄λ, μ€μΉν΄μ mongoDB λ₯Ό μ¬μ©νλ λ°©λ²μΌλ‘ λλμ΄μ Έ μμ΄μ,
λ°©λ²μ μ°Ύλλ° μ΄λ €μ λ€.
π» ν΄κ²°λ°©λ²
λ§μ λ°©λ²λ€μ μλν΄λ΄€λ€.
λ¨Όμ commonjs λ°©μμ΄ μλ, ESM λ°©μμΌλ‘ λ³κ²½ν΄μ μ§νν΄ λ³΄μμΌλ λ°λ건 μμλ€.
μ€ν μ€λ²νλ‘μ°, λ€λ₯Έ λΈλ‘κ·Έλ€ μμ μΆμ²ν΄μ£Όλ λ°©λ²λ μλν΄ λ΄€μ§λ§ μ¬μ ν λ°λλκ² μμκ³ ,
곡μλ¬Έμλ κ·Έλ¬λ€.
λκ° λ¬Έμ μΌκΉ... μΆμλ€.
곡μλ¬Έμμ λμ¨λλ‘ νλ©΄ λμ΄μΌ λ§λκ±΄λ° , λΆλͺ λ΄κ° λκ° μλͺ»νκ³ μμλ€.
νΉμ λͺ°λΌμ 곡μ λ¬Έμμ λμ¨ μ½λ κ·Έλλ‘, μ무κ²λ 건λλ¦¬μ§ μκ³ ,
κ·Έ μ체λ₯Ό κ°μ Έμμ λΆμ¬λ£μ΄λ³΄λ....
μ λ΅.....!!!!!
λλμ΄ λ°μ΄ν°κ° μ λ ₯λμλ€...π€ π€ π€ π€ π€ π€ π€
βοΈβοΈβοΈβοΈβοΈβοΈβοΈβοΈβοΈβοΈκ·Έλ°λ°....... μ΄κ² μλμλ€..βοΈβοΈβοΈβοΈβοΈβοΈβοΈβοΈβοΈβοΈβοΈ
λ΄ μ½λλ₯Ό λ³Έ λ€λ₯Έ λΆλ€μ΄ μ§κΈ λκ° μ΄μνλ€κ³ μλ €μ£Όμ ¨λ€.
mongooseλ₯Ό μ΄μ©ν΄μ ν΄λΌμ°λ νκ²½μμ λ°μ΄ν° κ°μ Έμ€κ³ μλλ°,
mongodb μ mongoClient λ₯Ό μ΄μ©ν΄μ λ νλ² μ°κ²°νκ³ μμλ€.....
λ°μ΄ν° μ λ ₯ νΉμ λΆλ¬μ€λ λͺ λ Ήμ΄κ° μλ¨Ήμ΄μ λκ° μ°κ²°μ΄ μ λλ‘ μλκ±°λΌκ³ μκ°νλλ°,
λ°μ΄ν° μ§μ΄λ£κ³ , μΆλ ₯νλ λͺ λ Ήμ΄ μ체λ₯Ό μ΄μνκ² νκ³ μμλ€.
db.collection.insertOne μ΄ μλ!!
λΌμ°ν°μμ κ°μ Έμ¨ νμΌμ λ΄μ λ³μμ λ΄μν
κ·Έ λ³μμλ€κ°.find() λ₯Ό ν΅ν΄ κ°μ Έμ€κ³ ,
κ·Έ λ³μμλ€κ° .create λ₯Ό ν΅ν΄ λ£μ΄μ£Όλ κ².....
μ΄λ κ² νλ μ λ§ μ μλμ΄ λμλ€. ...
mongooseλ mongodbμ μ¬μ΄νΈμ λΌμ΄λΈλ¬λ¦¬ μΉΈμ κ°μΌ μλ€..
https://mongoosejs.com/docs/index.html
Mongoose v8.0.0: Getting Started
First be sure you have MongoDB and Node.js installed. Next install Mongoose from the command line using npm: npm install mongoose --save Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first thing we need to do is
mongoosejs.com
λ΄κ° μμ±ν μ½λλ μ΄λ λ€.
products.schema.js
const mongoose = require("mongoose");
const productSchema = new mongoose.Schema({
productName: {
type: String,
required: true,
}
},
{timestamps : true }
);
module.exports = mongoose.model("carotMarket", productSchema);
index.js
require("dotenv").config();
const mongoose = require("mongoose");
const {DB_USER, DB_PW, DB_NAME} = process.env;
const connect = () => {
// mongoose.connectλ MongoDB μλ²μ μ°κ²°νλ λ©μλμ
λλ€.
mongoose
.connect(
`mongodb+srv://${DB_USER}:${DB_PW}@cluster0.pqqngbq.mongodb.net/${DB_NAME}`,
{autoIndex: false}
)
.then(() => console.log("MongoDB μ°κ²°μ μ±κ³΅νμμ΅λλ€."))
.catch((err) => console.log(`MongoDB μ°κ²°μ μ€ν¨νμμ΅λλ€. ${err}`));
};
mongoose.connection.on("error", (err) => {
console.error("MongoDB μ°κ²° μλ¬", err);
});
// export default connect;
module.exports = connect;
products.router.js
const express = require("express");
const router = express.Router();
const carotMarket = require("../schemas/products.schema.js");
//μ μ΄μ¨ νμ
λ³ν μν΄ μ
ν
router.use(express.json());
//λ°μ΄ν° μμΈ μ‘°ννκΈ°.
router.get("/products/:_id", async(req, res) => {
const { _id } = req.params;
let data = await carotMarket.find({_id: _id});
await res.json(data)
});
μ΄λ°μμΌλ‘ μ¬μ©νλ©΄ λ!!!
π§
λ§μ½ λͺ½κ³ ν΄λΌμ΄μΈνΈλ‘ μ°κ²°νκ³ μ νλ€λ©΄...πΉ
const { MongoClient } = require("mongodb");
// Replace the following with your Atlas connection string
const url = "<connection-string>"
const client = new MongoClient(url);
// Reference the database to use
const dbName = "gettingStarted";
async function run() {
try {
// Connect to the Atlas cluster
await client.connect();
const db = client.db(dbName);
// Reference the "people" collection in the specified database
const col = db.collection("people");
// Create a new document
let personDocument = {
"name": { "first": "Alan", "last": "Turing" },
"birth": new Date(1912, 5, 23), // May 23, 1912
"death": new Date(1954, 5, 7), // May 7, 1954
"contribs": [ "Turing machine", "Turing test", "Turingery" ],
"views": 1250000
}
// Insert the document into the specified collection
const p = await col.insertOne(personDocument);
// Find and return the document
const filter = { "name.last": "Turing" };
const document = await col.findOne(filter);
console.log("Document found:\n" + JSON.stringify(document));
} catch (err) {
console.log(err.stack);
}
finally {
await client.close();
}
}
run().catch(console.dir);
Insert and View a Document — MongoDB Atlas
Docs Home → MongoDB Atlas Estimated completion time: 5 minutesAfter you've connected to your Atlas cluster, you can interact with it. In this tutorial, you insert data into your cluster and read the new data by using mongosh, the Atlas UI, MongoDB Compas
www.mongodb.com
'TIL' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
23/11/05 TIL __ app.use(express.urlencoded({extended: true})); (0) | 2023.11.05 |
---|---|
23/11/04 TIL __ AWS λ°°ν¬νκΈ° μ΄μ 리2 (0) | 2023.11.04 |
23/11/02 TIL __ AWS λ°°ν¬νκΈ° μ΄μ 리 (1) | 2023.11.02 |
23/11/01 TIL __ Thunder Client 무νλ‘λ© νμ (1) | 2023.11.01 |
23/10/31 TIL __ λͺ¨λ νμ μμ λ³μμ νΉμ§ (0) | 2023.10.31 |