이를 위해 $type과 함께 $not 연산자를 사용할 수 있습니다. 먼저 문서로 컬렉션을 생성해 보겠습니다. −
> db.highestNumericValueOfAColumnDemo.insertOne(
... {
... "StudentName": "John",
... "StudentMathMarks":69
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cba05727219729fde21ddb1")
}
> db.highestNumericValueOfAColumnDemo.insertOne(
... {
... "StudentName": "Carol",
... "StudentMathMarks":"89"
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cba059d7219729fde21ddb2")
}
> db.highestNumericValueOfAColumnDemo.insertOne(
... {
... "StudentName": "Chris",
... "StudentMathMarks":82
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cba059d7219729fde21ddb3")
}
> db.highestNumericValueOfAColumnDemo.insertOne(
... {
... "StudentName": "John",
... "StudentMathMarks":"100"
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cba059d7219729fde21ddb4")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.highestNumericValueOfAColumnDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cba05727219729fde21ddb1"),
"StudentName" : "John",
"StudentMathMarks" : 69
}
{
"_id" : ObjectId("5cba059d7219729fde21ddb2"),
"StudentName" : "Carol",
"StudentMathMarks" : "89"
}
{
"_id" : ObjectId("5cba059d7219729fde21ddb3"),
"StudentName" : "Chris",
"StudentMathMarks" : 82
}
{
"_id" : ObjectId("5cba059d7219729fde21ddb4"),
"StudentName" : "John",
"StudentMathMarks" : "100"
} 다음은 열의 가장 높은 숫자 값을 찾는 쿼리입니다 -
> db.highestNumericValueOfAColumnDemo.find({StudentMathMarks: {$not: {$type:
2}}}).sort({StudentMathMarks: -1}).limit(1).pretty(); 이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cba059d7219729fde21ddb3"),
"StudentName" : "Chris",
"StudentMathMarks" : 82
} 위의 쿼리는 문자열 값을 무시하므로 가장 높은 정수 값인 82만 얻습니다.