MongoDB-তে জাভাস্ক্রিপ্ট এক্সপ্রেশন দিয়ে কমপ্লেক্স শর্ত চেক করতে $where অপারেটর ব্যবহার করা হয়। এটি স্লো হওয়া সত্ত্বেও ফ্লেক্সিবিলিটি দেয়। সতর্কতা: $where ব্যবহার করা উচিত না যতক্ষণ না অত্যন্ত জরুরি, কারণ এটি ইন্ডেক্স ব্যবহার করে না এবং সিকিউরিটি রিস্ক থাকতে পারে।
উদাহরণ: বিলিং এবং ডেলিভারি ঠিকানা
> db.demo589.insertOne({deliveryAddress: "US", billingAddress: "UK"});
> db.demo589.insertOne({deliveryAddress: "US", billingAddress: "US"});
> db.demo589.insertOne({deliveryAddress: "US", billingAddress: "AUS"});
> db.demo589.insertOne({deliveryAddress: "UK", billingAddress: "US"});
$where দিয়ে কোয়েরি: deliveryAddress == billingAddress
> db.demo589.find( { $where: "this.deliveryAddress == this.billingAddress" } );
আউটপুট:
{ "_id" : ..., "deliveryAddress" : "US", "billingAddress" : "US" }
বিকল্প (ফাস্টার, ইন্ডেক্স-ফ্রেন্ডলি):
// $expr ব্যবহার করে (MongoDB 3.6+)
db.demo589.find({ $expr: { $eq: ["$deliveryAddress", "$billingAddress"] } });
// অথবা সিম্পল কোয়েরি (যদি মান জানা থাকে)
db.demo589.find({ deliveryAddress: "US", billingAddress: "US" });
সুপারিশ: সম্ভব হলে $expr বা সাধারণ কোয়েরি ব্যবহার করুন; $where শুধু জাভাস্ক্রিপ্ট লজিক (রেজেক্স, ম্যাথ, ইত্যাদি) প্রয়োজন হলে।