$exists - Amazon DocumentDB

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

$exists

$exists산자는 필드가 문서에 존재하는지 여부를 확인하는 데 사용됩니다. 인덱싱된 필드가 모든 문서에 존재하지 않을 수 있는 Amazon DocumentDB의 희소 인덱스로 작업할 때 $exists 특히 유용합니다.

쿼리에서 생성된 희소 인덱스를 사용하려면 인덱스를 포함하는 필드에서 $exists 절을 사용해야 합니다. 를 생략하면 $exists Amazon DocumentDB는 쿼리에 희소 인덱스를 사용하지 않습니다.

파라미터

  • field: 존재 여부를 확인할 필드 이름입니다.

  • value: 필드가 일치하는 문서에 존재하는지(truefalse) 또는 존재하지 않는지(true) 지정하는 부울 값( 또는 false)입니다.

예제(MongoDB 쉘)

다음 예제에서는 food 컬렉션의 special_diets 필드에 희소 인덱스가 있는 $exists 연산자를 사용하는 방법을 보여줍니다.

샘플 문서 생성

db.food.insertMany([ { _id: 1, name: "Apple", special_diets: ["vegetarian", "gluten-free"] }, { _id: 2, name: "Broccoli" }, { _id: 3, name: "Chicken", special_diets: ["dairy-free"] } ]);

'special_diets' 필드에 희소 인덱스 생성

db.food.createIndex({ "special_diets": 1 }, { sparse: true, name: "special_diets_sparse_asc" });

쿼리 예제

db.food.find({ "special_diets": { $exists: true } });

출력

[ { "_id" : 1, "name" : "Apple", "special_diets" : [ "vegetarian", "gluten-free" ] }, { "_id" : 3, "name" : "Chicken", "special_diets" : [ "dairy-free" ] } ]

코드 예제

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

Node.js
const { MongoClient } = require('mongodb'); async function main() { 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('food'); const result = await collection.find({ "special_diets": { $exists: true } }).toArray(); console.log(result); await client.close(); } main();
Python
import pymongo client = pymongo.MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['food'] result = list(collection.find({"special_diets": {"$exists": True}})) print(result) client.close()