기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$not
연$not산자는 지정된 표현식의 결과를 무효화하는 데 사용됩니다. 지정된 조건이 일치하지 않는 문서를 선택할 수 있습니다.
플래너 버전 2.0에 $not {eq} 및에 대한 인덱스 지원이 추가되었습니다$not {in}.
파라미터
예제(MongoDB 쉘)
다음 예제에서는 $not 연산자를 사용하여 status 필드가 "활성"과 같지 않은 문서를 찾는 방법을 보여줍니다.
샘플 문서 생성
db.users.insertMany([
{ name: "John", status: "active" },
{ name: "Jane", status: "inactive" },
{ name: "Bob", status: "pending" },
{ name: "Alice", status: "active" }
]);
쿼리 예제
db.users.find({ status: { $not: { $eq: "active" } } });
출력
[
{
_id: ObjectId('...'),
name: 'Jane',
status: 'inactive'
},
{
_id: ObjectId('...'),
name: 'Bob',
status: 'pending'
}
]
코드 예제
$not 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.
- Node.js
-
const { MongoClient, Filters } = 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('users');
const result = await collection.find({
status: { $not: { $eq: "active" } }
}).toArray();
console.log(result);
await client.close();
}
main();
- Python
-
from pymongo import MongoClient
from bson.son import SON
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.find({
"status": {"$not": {"$eq": "active"}}
})
for doc in result:
print(doc)
client.close()