MongoDB에서 null 값을 쿼리하려면 $ne 연산자를 사용합니다. 먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.queryingNullDemo.insertOne(
... {
... "StudentName":"Larry",
... "StudentDetails":
... {
... "StudentAge":21,
... "StudentSubject":"MongoDB"
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd00bec588d4a6447b2e05f")
}
> db.queryingNullDemo.insertOne(
... {
... "StudentName":"Sam",
... "StudentDetails":
... {
... "StudentAge":23,
... "StudentSubject":null
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd00c00588d4a6447b2e060")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.queryingNullDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cd00bec588d4a6447b2e05f"),
"StudentName" : "Larry",
"StudentDetails" : {
"StudentAge" : 21,
"StudentSubject" : "MongoDB"
}
}
{
"_id" : ObjectId("5cd00c00588d4a6447b2e060"),
"StudentName" : "Sam",
"StudentDetails" : {
"StudentAge" : 23,
"StudentSubject" : null
}
} 다음은 null −
을 쿼리하는 방법입니다.> db.queryingNullDemo.find({'StudentDetails.StudentSubject': {$ne: null}}); 이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5cd00bec588d4a6447b2e05f"),
"StudentName" : "Larry",
"StudentDetails" : { "StudentAge" : 21, "StudentSubject" : "MongoDB" } }