count() মেথড ব্যবহার
কলেকশনে মোট কতগুলো ডকুমেন্ট আছে জানার জন্য count() মেথড ব্যবহার করা হয়।
সিনট্যাক্স
let myCollection = db.getCollection('yourCollectionName');
myCollection.count();
উদাহরণ: ডকুমেন্ট তৈরি
> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Bob"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9a5e2015e86fd1496b38a1") }
> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Ramit","CustomerAge":23});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9a5e3515e86fd1496b38a2") }
> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Adam","CustomerAge":27,"CustomerCountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9a5e4c15e86fd1496b38a3") }
কাউন্ট করা
> let myCollectionName = db.getCollection('countNumberOfDocumentsDemo');
> myCollectionName.count();
আউটপুট: 3
শর্তসাপেক্ষ কাউন্ট
db.collection.count({ "CustomerAge": { $gt: 25 } }); // 25 এর বেশি বয়স वाले
গুরুত্বপূর্ণ: MongoDB 4.0+ এ count() ডিপ্রিকেটেড। ব্যবহার করুন:
countDocuments(query)- সঠিক কাউন্ট (ফিল্টার সাপোর্ট)estimatedDocumentCount()- দ্রুত এস্টিমেট (কোনো ফিল্টার নেই)