필드가 MongoDB에서 숫자인지 확인하려면 $type 연산자를 사용하십시오. 다음은 구문입니다.
db.yourCollectionName.find({youtFieldName: {$type:"number"}}).pretty(); 먼저 문서로 컬렉션을 생성하겠습니다.
> db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"John","StudentAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ec75dd628fa4220163b83")
}
>db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"Chris","StudentMathScore":98,"StudentCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ec77cd628fa4220163b84")
}
>db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"Robert","StudentCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ec7a4d628fa4220163b85")
}
>db.checkIfFieldIsNumberDemo.insertOne({"StudentId":101,"StudentName":"Larry","StudentCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ec7ccd628fa4220163b86")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다.
> db.checkIfFieldIsNumberDemo.find().pretty();
그러면 다음과 같은 출력이 생성됩니다.
{
"_id" : ObjectId("5c9ec75dd628fa4220163b83"),
"StudentName" : "John",
"StudentAge" : 23
}
{
"_id" : ObjectId("5c9ec77cd628fa4220163b84"),
"StudentName" : "Chris",
"StudentMathScore" : 98,
"StudentCountryName" : "US"
}
{
"_id" : ObjectId("5c9ec7a4d628fa4220163b85"),
"StudentName" : "Robert",
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c9ec7ccd628fa4220163b86"),
"StudentId" : 101,
"StudentName" : "Larry",
"StudentCountryName" : "AUS"
} 다음은 필드가 숫자인지 확인하는 쿼리입니다.
> db.checkIfFieldIsNumberDemo.find({StudentMathScore: {$type:"number"}}).pretty(); 다음은 숫자인 필드(예:StudentMathScore
)를 표시하는 출력입니다.{
"_id" : ObjectId("5c9ec77cd628fa4220163b84"),
"StudentName" : "Chris",
"StudentMathScore" : 98,
"StudentCountryName" : "US"
}