TIL

23/11/03 TIL __ mongodb μ•„ν‹€λΌμŠ€ db.collection μ•ˆλ˜λŠ” 이유 ...

GABOJOK 2023. 11. 3. 19:49

 

 

 

🧐   μƒν™©

 

κ°œμΈκ³Όμ œκ°€ μ‹œμž‘λ˜μ—ˆλ‹€. 

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);

 

 

 

https://www.mongodb.com/docs/atlas/tutorial/insert-data-into-your-cluster/#std-label-gswa-insert-data

 

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