

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# \$1inc
<a name="inc"></a>

`$inc` 연산자는 필드 값을 지정된 양만큼 늘리는 데 사용됩니다. 현재 값을 검색하고 새 값을 계산한 다음 필드를 업데이트할 필요 없이 카운터 또는 등급과 같은 숫자 필드를 업데이트하는 데 사용됩니다.

**파라미터**
+ `field`: 늘릴 필드의 이름입니다.
+ `amount`: 필드를 늘릴 양입니다. 양수 또는 음수 값일 수 있습니다.

## 예제(MongoDB 쉘)
<a name="inc-examples"></a>

다음 예제에서는 `$inc` 연산자를 사용하여 문서의 `age` 필드를 늘리는 방법을 보여줍니다.

**샘플 문서 생성**

```
db.users.insertOne({_id: 123, name: "John Doe", age: 30})
```

**쿼리 예제**

```
db.users.updateOne({_id: 123}, {$inc: {age: 1}})
```

**업데이트된 문서 보기**

```
db.users.findOne({_id: 123})
```

**출력**

```
{ "_id" : 123, "name" : "John Doe", "age" : 31 }
```

## 코드 예제
<a name="inc-code"></a>

`$inc` 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function updateWithInc() {
  const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
  const db = client.db('test');
  const collection = db.collection('users');

  const result = await collection.updateOne(
    { _id: 123 },
    { $inc: { age: 1 } }
  );

  console.log(result);

  await client.close();
}

updateWithInc();
```

------
#### [ Python ]

```
from pymongo import MongoClient

def update_with_inc():
    client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
    db = client['test']
    collection = db['users']

    result = collection.update_one(
        {'_id': 123},
        {'$inc': {'age': 1}}
    )

    print(result.modified_count)

    client.close()

update_with_inc()
```

------