문제
데이터의 아이디는 삭제와 추가에 상관 없이 id의 영구결번이 있어야 한다.
순서
1. count라는 collection 추가
2. name: 총개수
countTotal: 0 객체 만들기
3. db.collection 3개
3.1 name: '총개수'인 객체에서 countTotal 속성 찾기 이 데이터를 result에 저장후 새로운 변수에 저장
3.2 같은 전역 안에 넣기 위해 insertOne을 findOne에 넣어주면 totalcount를 쓸 수 있음 post 하여 하나씩 넣어줄 때마다
totalCount에 1를 더하기
3.insertOne을 한 후에는 기존에 있던 속성 counter 에 속성 countTotal에 1을 더하기
app.post('/add',function(request,response){
response.send('잘 받았어!!')
db.collection('counter').findOne({name: '총개수'},function(error,result){
console.log(result.countTotal)
var totalcount= result.countTotal
db.collection('post').insertOne({
_id: totalcount +1 ,
아이디: request.body.name,
내용: request.body.content
},function(){
console.log("정보 잘들어왔어!!")
db.collection('counter').updateOne({name:'총개수'},{$inc :{ countTotal :1}},function(error,result){
if(error){
return console.log(error)
}
})
})
})
})
'MongoDB' 카테고리의 다른 글
html 데이터 삭제 방법 (0) | 2021.10.06 |
---|---|
db 내용 html에 보여주기 (0) | 2021.10.05 |
고객의 post 요청 db에 담기 (0) | 2021.10.04 |
몽고DB 설치부터 ~서버에서 db 넣어보기 (0) | 2021.10.04 |