MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

フィールドがMongoDBの数値であるかどうかを確認するにはどうすればよいですか?


フィールドが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"
}

  1. MongoDBコレクションの空のフィールドをチェックする方法は?

    MongoDBコレクションの空のフィールドを確認するには、$existsと$eq演算子を併用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo485.insertOne({"FirstName":"Chris","LastName":""});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82e9f6b0f3fa88e22

  2. 数値がC#で2の累乗であるかどうかを確認するにはどうすればよいですか?

    2の累乗は、2nの形式の数値です。nは整数です 2を底とし、整数nを指数とするべき乗の結果。 n 2n 0 1 1 2 2 4 3 8 4 16 5 32 例1 class Program {    static void Main() {       Console.WriteLine(IsPowerOfTwo(9223372036854775809));       Console.WriteLine(IsPowe