

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

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

`$nin` 연산자는 지정된 배열에 없는 값을 일치시키는 데 사용됩니다. 연`$in`산자의 역으로, 지정된 배열에 있는 값과 일치합니다.

플래너 버전 2.0에에 대한 인덱스 지원이 추가되었습니다`$nin`.

**파라미터**
+ `field`: 확인할 필드입니다.
+ `array`: 확인할 값의 배열입니다.

 

**필드 이름의 달러(`$`)**

중첩된 객체에서의 `$` 접두사 필드 쿼리`$nin`와 관련된 [필드 이름의 달러(\$1) 및 점(.)](functional-differences.md#functional-differences-dollardot) 제한 사항은 섹션을 참조하세요.

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

다음 예제에서는 `$nin` 연산자를 사용하여 `category` 필드가 "Fiction" 또는 "Mystery"와 같지 않은 문서를 찾는 방법을 보여줍니다.

**샘플 문서 생성**

```
db.books.insertMany([
  { title: "The Great Gatsby", author: "F. Scott Fitzgerald", category: "Fiction" },
  { title: "To Kill a Mockingbird", author: "Harper Lee", category: "Fiction" },
  { title: "The Girl on the Train", author: "Paula Hawkins", category: "Mystery" },
  { title: "The Martian", author: "Andy Weir", category: "Science Fiction" },
  { title: "The Alchemist", author: "Paulo Coelho", category: "Philosophy" }
])
```

**쿼리 예제**

```
db.books.find({
  category: {
    $nin: ["Fiction", "Mystery"]
  }
})
```

**출력**

```
[
  {
    _id: ObjectId('...'),
    title: 'The Martian',
    author: 'Andy Weir',
    category: 'Science Fiction'
  },
  {
    _id: ObjectId('...'),
    title: 'The Alchemist',
    author: 'Paulo Coelho',
    category: 'Philosophy'
  }
]
```

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

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

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

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

async function findBooksNotInCategories(categories) {
  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 books = await db.collection('books').find({
    category: {
      $nin: categories
    }
  }).toArray();
  console.log(books);
  client.close();
}

findBooksNotInCategories(['Fiction', 'Mystery']);
```

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

```
from pymongo import MongoClient

def find_books_not_in_categories(categories):
    client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
    db = client['test']
    books = list(db.books.find({
        'category': {
            '$nin': categories
        }
    }))
    print(books)
    client.close()

find_books_not_in_categories(['Fiction', 'Mystery'])
```

------