MongoDB-তে ডেটা খোঁজার জন্য query operators ব্যবহার করা হয়। Java MongoDB ড্রাইভারও এই অপারেটরগুলিকে সমর্থন করে, এবং এগুলি ব্যবহার করে আপনি MongoDB তে বিভিন্ন query তৈরি করতে পারেন। MongoDB এর জন্য কিছু সাধারণ query operators হল:
- eq(): সমান
- ne(): সমান নয়
- gt(): বড়
- lt(): ছোট
- in(): মধ্যে
- 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:
- eq() - সমান (Equality):
eq()অপারেটর MongoDB তে ইকুয়াল বা সমান মানের অনুসন্ধান করতে ব্যবহৃত হয়। Java-তেnew Document("field", value)ব্যবহার করে এটি কার্যকর করা হয়।- Example:
new Document("age", 30)→ এটি age ফিল্ডের মান 30 এর সমান ডকুমেন্ট খুঁজে বের করবে।
- ne() - সমান নয় (Not Equal):
ne()অপারেটর MongoDB তে সমান নয় এর জন্য ব্যবহৃত হয়। Java-তে এটি$neব্যবহার করে লেখা হয়।- Example:
new Document("age", new Document("$ne", 30))→ এটি age ফিল্ডের মান 30 এর সমান নয় এমন ডকুমেন্ট খুঁজে বের করবে।
- gt() - বড় (Greater Than):
gt()অপারেটর MongoDB তে বড় মানের অনুসন্ধান করতে ব্যবহৃত হয়, অর্থাৎ নির্দিষ্ট মানের চেয়ে বড় মান খোঁজা হয়। Java-তে$gtব্যবহার করা হয়।- Example:
new Document("age", new Document("$gt", 25))→ এটি age ফিল্ডের মান 25 এর চেয়ে বড় এমন ডকুমেন্ট খুঁজে বের করবে।
- lt() - ছোট (Less Than):
lt()অপারেটর MongoDB তে ছোট মানের অনুসন্ধান করতে ব্যবহৃত হয়। Java-তে$ltব্যবহার করা হয়।- Example:
new Document("age", new Document("$lt", 30))→ এটি age ফিল্ডের মান 30 এর চেয়ে ছোট এমন ডকুমেন্ট খুঁজে বের করবে।
- in() - মধ্যে (In):
in()অপারেটর MongoDB তে একটি নির্দিষ্ট মানের মধ্যে থাকা ডকুমেন্ট খুঁজতে ব্যবহৃত হয়। Java-তে$inব্যবহার করা হয়।- Example:
new Document("age", new Document("$in", new int[]{25, 30}))→ এটি age ফিল্ডের মান যেগুলি 25 বা 30 এর মধ্যে, সেগুলি খুঁজে বের করবে।
- 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:
| Operator | Description | MongoDB Query Example |
|---|---|---|
eq() | Equal to | new Document("age", 30) |
ne() | Not equal to | new Document("age", new Document("$ne", 30)) |
gt() | Greater than | new Document("age", new Document("$gt", 25)) |
lt() | Less than | new Document("age", new Document("$lt", 30)) |
in() | In a set of values | new Document("age", new Document("$in", new int[]{25, 30})) |
nin() | Not in a set of values | new 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 ব্যবহার করে ডকুমেন্টের মধ্যে নির্দিষ্ট মান অনুসন্ধান করা যায়, যা কোড লেখার সময় খুবই কার্যকরী।
Read more