프로그래밍/MongoDB

몽고디비 설치

타코코딩 2023. 12. 5. 15:18
npm i mongodb

몽고디비설치

홈페이지에서 db와 콜렉션명을 생성한다
테스트용으로 글하나 만들어보기

 

const { MongoClient, ServerApiVersion } = require("mongodb");
const uri =
  "mongodb+srv://admin:qwer1234@admin.eepuvsg.mongodb.net/?retryWrites=true&w=majority";

// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
  serverApi: {
    version: ServerApiVersion.v1,
    strict: true,
    deprecationErrors: true,
  },
});

async function run() {
  try {
    // Connect the client to the server	(optional starting in v4.7)
    await client.connect();
    // Send a ping to confirm a successful connection
    await client.db("admin").command({ ping: 1 });
    console.log(
      "Pinged your deployment. You successfully connected to MongoDB!"
    );
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

export { client };

몽고디비 버전에 따른 사용코드 홈페이지에서 복붙

https://www.mongodb.com/ko-kr

 

MongoDB: 애플리케이션 데이터 플랫폼

업계 최고의 최신 데이터베이스를 토대로 구축된 애플리케이션 데이터 플랫폼을 사용해 아이디어를 더욱 빠르게 실현하세요. MongoDB는 데이터를 손쉽게 처리할 수 있도록 지원합니다.

www.mongodb.com

 

  const clients = await client;
  const db = clients.db("jdb");
  let result = await db.collection("post").find().toArray();
  console.log(result);

db (jdb)와 컬렉션명(post)을 위와 같이 입력하고 콘솔 찍어보기

 

결과가 터미널 콘솔에 잘 찍히면 연결이 잘된 것

'프로그래밍 > MongoDB' 카테고리의 다른 글

몽고디비 next js 연동  (0) 2023.12.07
몽고디비 글작성 insertOne  (1) 2023.12.07