यह जांचने के लिए कि क्या 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")
} खोज () विधि की सहायता से संग्रह से सभी दस्तावेज़ों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है
> 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"
}