# 게시물마다 번호를 달아 저장하기2

&#x20;**DB 데이터를 수정하고 싶으면 updateOne을 쓰시면 됩니다.**&#x20;

counter라는 콜렉션 내의 자료를 수정하고 싶으면 이렇게 하시면 됩니다.

```javascript
db.collection('counter').updateOne( {요런 이름의 자료를} , {이렇게 수정해주세요} , function(에러, 결과){
  console.log('수정완료')
})
```

updateOne 함수엔 파라미터가 세개가 필요합니다. \
**왼쪽**엔 { name : ‘게시물갯수’ } 이렇게 자료를 찾을 수 있는 이름이라든지 쿼리문을 적어주면 됩니다.&#x20;

**가운데**는 여러분이 수정할 값을 입력해주시면 됩니다. 그런데 약간 특이합니다. \
\&#xNAN;**{ $set : { totalPost : 100 } }** 이렇게 넣어서 값을 아예 100으로 변경할 수도 있고\
\&#xNAN;**{ $inc : { totalPost : 5 } }** 이렇게 넣어서 값을 5만큼 더해줄 수도 있습니다. \
$ 표시 붙은게 바로 operator 라는 문법입니다. 여러 종류가 있으니 나머지는 필요할 때 찾아쓰도록 합시다.&#x20;

**오른쪽**은 그냥 콜백함수입니다. 수정이 실패나 성공시 실행할 코드를 안에 담으시면 됩니다.&#x20;

&#x20;**그럼 데이터를 1 증가시키려면 이렇게 하면 되겠군요.**

![▲ 위의 totalPost라는 데이터를 1 증가시켜봅시다. ](/files/-M4NPPbda2gBIvbvJ0LO)

```javascript
db.collection('counter').updateOne( {name : '게시물갯수' } , { $inc : { totalPost : 1 } } , function(에러, 결과){
  console.log('수정완료')
})
```

이 코드를 실행하면 totalPost라는 항목이 1 증가합니다. 끝!

### 컨텐츠를 하나 발행할 때 아래 코드를 실행 시켜주세요!!.

```javascript
app.post('/add', function (요청, 응답) {
  db.collection('counter').findOne({name : '게시물갯수'}, function(에러, 결과){
    var 총게시물갯수 = 결과.totalPost

    db.collection('post').insertOne({ _id : 총게시물갯수 + 1, 제목 : 요청.body.title, 날짜 : 요청.body.date }, function (에러, 결과) {
      db.collection('counter').updateOne({name:'게시물갯수'},{ $inc: {totalPost:1} },function(에러, 결과){
	      if(에러){return console.log(에러)}
          응답.send('전송완료');
        })
      })

  })
})

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://triplexlab.gitbook.io/vanilla-js/rest-api/2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
