$nin - Amazon DocumentDB

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

$nin

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

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

파라미터

  • field: 확인할 필드입니다.

  • array: 확인할 값의 배열입니다.

 

필드 이름의 달러($)

중첩된 객체에서의 $ 접두사 필드 쿼리$nin와 관련된 필드 이름의 달러($) 및 점(.) 제한 사항은 섹션을 참조하세요.

예제(MongoDB 쉘)

다음 예제에서는 $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' } ]

코드 예제

$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'])