- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- nginx
- clipBehavior
- css
- mongoose
- RDS
- double quote
- findByIdAndDelete
- til
- Nodejs
- AWS
- TypeScript
- wil
- mongodb
- await
- jsonwebtoken
- Express
- async
- https
- EC2
- TailwindCSS
- flutter
- MYSQL
- certbot
- moment
- JavaScript
- atlas
- Find
- single quote
- sequelize
- Node.js
목록mongoose (10)
기억 휘발 방지소
📌 실습코드 아래 코드와 결과를 갖고 skip()과 limit()를 알아볼 것이다. title이 test1~5까지 들어있다. 코드 const posts = await Post.find({}); console.log(posts); 검색결과 [ { _id: new ObjectId("6167df634f117c17b2ea0b5a"), title: 'test1', __v: 0 }, { _id: new ObjectId("616821f7249553ee8393dce8"), title: 'test2', __v: 0 }, { _id: new ObjectId("616821fe249553ee8393dcea"), title: 'test3', __v: 0 }, { _id: new ObjectId("6168220a249553ee8..
📌 오류 작성한 코드는 다음과 같다. const mongoose = require("mongoose"); mongoose.connect(process.env.DB_URL); const db = mongoose.connection; db.on("error", (error) => console.log("❗ DB Error", error)); db.once("open", () => console.log("✅ Connected to DB!")); process.env.DB_URL이 들어가는 자리에는 Cluster에서 아래와 같이 생긴 주소를 넣어주면 된다. username, password 본인의 username과, 발급받은 password를 넣고 접속할 DB이름을 넣어주면 된다. mongodb+srv://:@..
📌 findByIdAndDelete 파라미터로는 문서의 _id가 들어온다. 문서의 _id필드를 찾아 삭제한다. Model.findOneAndDelete({ _id: id })의 약자라고 한다. await Model.findByIdAndDelete(_id); 📌 deleteOne 컬렉션에서 조건에 일치하는 첫 번째 문서를 삭제한다. await Model.deleteOne({ name: "삭제할 이름" }); 📌 deleteMany 컬렉션에서 조건과 일치하는 모든 문서를 삭제한다. await Model.deleteMany({ name: "삭제할 이름", age: 30 });
findByIdAndDelete()에 매개변수로는 id값이 들어온다. 몽고DB에 _id값 findOneAndDelete({ _id: id })를 줄인 버전이라고 할 수 있다. export const deletePost = async (req, res) => { const { id } = req.params; await Post.findByIdAndDelete(id); return res.redirect("/"); };
📌 findByIdAndUpdate를 쓰지 않고 수정하기 title과 description을 수정할거다. findById()로 DB에서 문서들을 가져오고 post.title = title; post.description = description; 으로 수정하려고하는 값들을 바꿔주고 post.save()를 해준다. export const postEdit = async (req, res) => { const { id } = req.params; const { title, description } = req.body; const post = await Post.findById(id); // 수정 post.title = title; post.description = description; await post.s..
검색을 할 때에도 2가지 방법으로 할 수 있다. findById() findOne() 📌 Model.findById(id) _id를 기준으로 단일 문서를 찾는다. _id를 기준으로 질의하려면 findOne()대신에 findById()를 사용하라고 한다. 참고문서 Mongoose v6.0.6: Parameters doc «Object» values for initial set optional «[fields]» object containing the fields that were selected in the query which returned this document. You do not need to set this parameter to ensure Mongoose handles your query ..