Simple Queries তৈরি করা (eq(), ne(), gt(), lt(), in(), nin())

Java Technologies - জাভা মঙ্গোডিবি (Java MongoDB) - Query এবং Filter ব্যবহার
142

MongoDB-তে ডেটা খোঁজার জন্য query operators ব্যবহার করা হয়। Java MongoDB ড্রাইভারও এই অপারেটরগুলিকে সমর্থন করে, এবং এগুলি ব্যবহার করে আপনি MongoDB তে বিভিন্ন query তৈরি করতে পারেন। MongoDB এর জন্য কিছু সাধারণ query operators হল:

  1. eq(): সমান
  2. ne(): সমান নয়
  3. gt(): বড়
  4. lt(): ছোট
  5. in(): মধ্যে
  6. nin(): মধ্যে নয়

এগুলি MongoDB এর query operators যেগুলি ব্যবহার করে ডকুমেন্টের মধ্যে নির্দিষ্ট মান অনুসন্ধান করা যায়। Java MongoDB ড্রাইভার ব্যবহার করে আপনি এগুলি প্রোগ্রামmatically ব্যবহার করতে পারেন।


1. MongoDB Query Operators এর জন্য Java Example

এখানে MongoDB তে বিভিন্ন query operator ব্যবহার করে ডেটা খোঁজার জন্য Java কোডের উদাহরণ দেওয়া হচ্ছে:

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.client.FindIterable;

public class MongoDBQueryExample {
    public static void main(String[] args) {
        // MongoDB সার্ভারের সাথে সংযোগ স্থাপন
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("testdb");

        // Collection অ্যাক্সেস করা
        MongoCollection<Document> collection = database.getCollection("users");

        // eq() - সমান (Equality)
        FindIterable<Document> resultEq = collection.find(new Document("age", 30));
        System.out.println("Results for eq() (age = 30):");
        for (Document doc : resultEq) {
            System.out.println(doc.toJson());
        }

        // ne() - সমান নয় (Not Equal)
        FindIterable<Document> resultNe = collection.find(new Document("age", new Document("$ne", 30)));
        System.out.println("Results for ne() (age != 30):");
        for (Document doc : resultNe) {
            System.out.println(doc.toJson());
        }

        // gt() - বড় (Greater Than)
        FindIterable<Document> resultGt = collection.find(new Document("age", new Document("$gt", 25)));
        System.out.println("Results for gt() (age > 25):");
        for (Document doc : resultGt) {
            System.out.println(doc.toJson());
        }

        // lt() - ছোট (Less Than)
        FindIterable<Document> resultLt = collection.find(new Document("age", new Document("$lt", 30)));
        System.out.println("Results for lt() (age < 30):");
        for (Document doc : resultLt) {
            System.out.println(doc.toJson());
        }

        // in() - মধ্যে (In)
        FindIterable<Document> resultIn = collection.find(new Document("age", new Document("$in", new int[]{25, 30})));
        System.out.println("Results for in() (age in [25, 30]):");
        for (Document doc : resultIn) {
            System.out.println(doc.toJson());
        }

        // nin() - মধ্যে নয় (Not In)
        FindIterable<Document> resultNin = collection.find(new Document("age", new Document("$nin", new int[]{25, 30})));
        System.out.println("Results for nin() (age not in [25, 30]):");
        for (Document doc : resultNin) {
            System.out.println(doc.toJson());
        }

        // MongoDB সার্ভারে সংযোগ বন্ধ করা
        mongoClient.close();
    }
}

Explanation of Each Query Operator:

  1. eq() - সমান (Equality):
    • eq() অপারেটর MongoDB তে ইকুয়াল বা সমান মানের অনুসন্ধান করতে ব্যবহৃত হয়। Java-তে new Document("field", value) ব্যবহার করে এটি কার্যকর করা হয়।
    • Example: new Document("age", 30) → এটি age ফিল্ডের মান 30 এর সমান ডকুমেন্ট খুঁজে বের করবে।
  2. ne() - সমান নয় (Not Equal):
    • ne() অপারেটর MongoDB তে সমান নয় এর জন্য ব্যবহৃত হয়। Java-তে এটি $ne ব্যবহার করে লেখা হয়।
    • Example: new Document("age", new Document("$ne", 30)) → এটি age ফিল্ডের মান 30 এর সমান নয় এমন ডকুমেন্ট খুঁজে বের করবে।
  3. gt() - বড় (Greater Than):
    • gt() অপারেটর MongoDB তে বড় মানের অনুসন্ধান করতে ব্যবহৃত হয়, অর্থাৎ নির্দিষ্ট মানের চেয়ে বড় মান খোঁজা হয়। Java-তে $gt ব্যবহার করা হয়।
    • Example: new Document("age", new Document("$gt", 25)) → এটি age ফিল্ডের মান 25 এর চেয়ে বড় এমন ডকুমেন্ট খুঁজে বের করবে।
  4. lt() - ছোট (Less Than):
    • lt() অপারেটর MongoDB তে ছোট মানের অনুসন্ধান করতে ব্যবহৃত হয়। Java-তে $lt ব্যবহার করা হয়।
    • Example: new Document("age", new Document("$lt", 30)) → এটি age ফিল্ডের মান 30 এর চেয়ে ছোট এমন ডকুমেন্ট খুঁজে বের করবে।
  5. in() - মধ্যে (In):
    • in() অপারেটর MongoDB তে একটি নির্দিষ্ট মানের মধ্যে থাকা ডকুমেন্ট খুঁজতে ব্যবহৃত হয়। Java-তে $in ব্যবহার করা হয়।
    • Example: new Document("age", new Document("$in", new int[]{25, 30})) → এটি age ফিল্ডের মান যেগুলি 25 বা 30 এর মধ্যে, সেগুলি খুঁজে বের করবে।
  6. nin() - মধ্যে নয় (Not In):
    • nin() অপারেটর MongoDB তে in() এর বিপরীত, অর্থাৎ একটি নির্দিষ্ট মানের মধ্যে না থাকা ডকুমেন্ট খুঁজতে ব্যবহৃত হয়। Java-তে $nin ব্যবহার করা হয়।
    • Example: new Document("age", new Document("$nin", new int[]{25, 30})) → এটি age ফিল্ডের মান যেগুলি 25 বা 30 এর মধ্যে নয়, সেগুলি খুঁজে বের করবে।

MongoDB Query Operators Summary:

OperatorDescriptionMongoDB Query Example
eq()Equal tonew Document("age", 30)
ne()Not equal tonew Document("age", new Document("$ne", 30))
gt()Greater thannew Document("age", new Document("$gt", 25))
lt()Less thannew Document("age", new Document("$lt", 30))
in()In a set of valuesnew Document("age", new Document("$in", new int[]{25, 30}))
nin()Not in a set of valuesnew Document("age", new Document("$nin", new int[]{25, 30}))

Java MongoDB ড্রাইভার ব্যবহার করে MongoDB তে simple queries তৈরি করা সম্ভব এবং বিভিন্ন query operators যেমন eq(), ne(), gt(), lt(), in(), এবং nin() ব্যবহার করে আপনি ডেটা অনুসন্ধান করতে পারবেন। MongoDB তে বিভিন্ন ধরনের query operators এবং filter conditions ব্যবহার করে ডকুমেন্টের মধ্যে নির্দিষ্ট মান অনুসন্ধান করা যায়, যা কোড লেখার সময় খুবই কার্যকরী।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...