Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MongoDB

MongoDB में फ़ील्ड मौजूद है या नहीं कैसे जांचें?

MongoDB में किसी फ़ील्ड की मौजूदगी जांचने के लिए $exists ऑपरेटर का उपयोग करें। यह ऑपरेटर उन दस्तावेज़ों को ढूंढता है जिनमें निर्दिष्ट फ़ील्ड मौजूद हो या न हो।

उदाहरण

कलेक्शन बनाएं और विभिन्न फ़ील्ड वाले दस्तावेज़ डालें:

> db.checkFieldExistsDemo.insertOne({"StudentFirstName":"John","StudentGender":"Male","StudentMongoDBScore":89});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd909611a844af18acdffbd") }
> db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Emma","StudentGender":"Female","StudentMongoDBScore":58});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd909781a844af18acdffbe") }
> db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Carol","StudentGender":"Male","StudentMongoDBScore":77});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd909871a844af18acdffbf") }
> db.checkFieldExistsDemo.insertOne({"StudentFirstName":"David","StudentMongoDBScore":98});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd909a31a844af18acdffc0") }

सभी दस्तावेज़ देखें:

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

आउटपुट:

{ "_id" : ObjectId("5cd909611a844af18acdffbd"), "StudentFirstName" : "John", "StudentGender" : "Male", "StudentMongoDBScore" : 89 }
{ "_id" : ObjectId("5cd909781a844af18acdffbe"), "StudentFirstName" : "Emma", "StudentGender" : "Female", "StudentMongoDBScore" : 58 }
{ "_id" : ObjectId("5cd909871a844af18acdffbf"), "StudentFirstName" : "Carol", "StudentGender" : "Male", "StudentMongoDBScore" : 77 }
{ "_id" : ObjectId("5cd909a31a844af18acdffc0"), "StudentFirstName" : "David", "StudentMongoDBScore" : 98 }

StudentGender फ़ील्ड न होने वाले दस्तावेज़ ढूंढें

> db.checkFieldExistsDemo.find(
  {"StudentMongoDBScore":98, "StudentGender":{"$exists": false}},
  {'StudentFirstName': 1, '_id':0}
);

आउटपुट:

{ "StudentFirstName" : "David" }

उपयोग:

  • {field: {$exists: true}} - फ़ील्ड मौजूद है
  • {field: {$exists: false}} - फ़ील्ड मौजूद नहीं है

यह ऑपरेटर नल (null) मान वाले फ़ील्ड से अलग व्यवहार करता है - $exists: true नल मान वाले फ़ील्ड के लिए भी सही लौटाता है।

  1. - C# में कैसे जांचें कि कोई थ्रेड जीवित है या नहीं

    यह जांचने के लिए कि कोई थ्रेड जीवित है या नहीं, कोड निम्नानुसार है: उदाहरण using System; using System.Threading; public class Demo { public static void Main() { Thread thread = new Thread(new ThreadStart(demo1)); thread = Thread.CurrentThread; Console.WriteLine(Is

  1. - C# में कैसे जांचें कि कोई थ्रेड बैकग्राउंड थ्रेड है या नहीं

    यह जांचने के लिए कि कोई थ्रेड बैकग्राउंड थ्रेड है या नहीं, कोड निम्नानुसार है: उदाहरण using System; using System.Threading; public class Demo { public static void Main() { Thread thread = new Thread(new ThreadStart(demo1)); ThreadPool.QueueUserWorkItem(new WaitCallback(dem

  1. - C# प्रोग्राम: जांचना कि डायरेक्टरी मौजूद है या नहीं

    डायरेक्टरी मौजूद है या नहीं, इसकी जांच करने के लिए Directory.Exists मेथड का उपयोग करें।मान लीजिए आपको जांचना है कि निम्नलिखित डायरेक्टरी मौजूद है या नहीं:C:\\Amitइसके लिए, Exists() मेथड का उपयोग करें:if (Directory.Exists("C:\\Amit")) {    Console.WriteLine("डायरेक्टरी Amit म