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

MongoDBで「NotLike」演算子を使用するにはどうすればよいですか?


これには、MongoDBの$not演算子を使用します。概念を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-

> db.notLikeOperatorDemo.insertOne({"StudentName":"John Doe"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a29c393b406bd3df60dfc")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"John Smith"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a29cc93b406bd3df60dfd")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"John Taylor"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a29df93b406bd3df60dfe")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"Carol Taylor"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a2a1693b406bd3df60dff")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"David Miller"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a2a2693b406bd3df60e00")
}

find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-

> db.notLikeOperatorDemo.find().pretty();

以下は出力です-

{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" }
{
   "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"),
   "StudentName" : "John Smith"
}
{
   "_id" : ObjectId("5c8a29df93b406bd3df60dfe"),
   "StudentName" : "John Taylor"
}
{
   "_id" : ObjectId("5c8a2a1693b406bd3df60dff"),
   "StudentName" : "Carol Taylor"
}
{
   "_id" : ObjectId("5c8a2a2693b406bd3df60e00"),
   "StudentName" : "David Miller"
}

これは、MongoDBでNOTLIKE演算子を使用するためのクエリです-

> db.notLikeOperatorDemo.find( { StudentName: { $not: /^John Taylor.*/ } } );

以下は出力です-

{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" }
{ "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"), "StudentName" : "John Smith" }
{ "_id" : ObjectId("5c8a2a1693b406bd3df60dff"), "StudentName" : "Carol Taylor" }
{ "_id" : ObjectId("5c8a2a2693b406bd3df60e00"), "StudentName" : "David Miller" }
You can use the following query also:
> db.notLikeOperatorDemo.find({StudentName: {$not: /John Taylor/}});
The following is the output:
{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" }
{ "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"), "StudentName" : "John Smith" }
{ "_id" : ObjectId("5c8a2a1693b406bd3df60dff"), "StudentName" : "Carol Taylor" }
{ "_id" : ObjectId("5c8a2a2693b406bd3df60e00"), "StudentName" : "David Miller" }

  1. C#で「is」演算子を使用するにはどうすればよいですか?

    C#の「is」演算子は、オブジェクトの実行時型が特定の型と互換性があるかどうかを確認します。 構文は次のとおりです- expr is type ここでは、 expr 式とタイプです タイプの名前です 以下は、C#でのis演算子の使用法を示す例です- 例 using System; class One { } class Two { } public class Demo {    public static void Test(object obj) {       One x;       Two y;

  2. C#で「as」演算子を使用するにはどうすればよいですか?

    「as」演算子は、互換性のあるタイプ間の変換を実行します。これはキャスト操作のようなものであり、参照変換、null許容変換、およびボクシング変換のみを実行します。 as演算子は、ユーザー定義の変換などの他の変換を実行できません。代わりに、キャスト式を使用して実行する必要があります。 以下は、C#でのas操作の使用法を示す例です。ここで「as」は変換に使用されます: string s = obj[i] as string; 次のコードを実行して、C#の「as」演算子を使用してみてください- 例 using System; public class Demo {    p