একটি অ্যারের আইটেমের সংখ্যা গণনা করতে, আপনি $size অপারেটর ব্যবহার করতে পারেন। সিনট্যাক্স নিম্নরূপ:
db.yourCollectionName.aggregate({$project:{anyFieldName:{$size:"$yourArrayName"}}}).prett
y(); উপরের সিনট্যাক্স বুঝতে, আসুন ডকুমেন্ট সহ একটি সংগ্রহ তৈরি করি। নথির সাথে একটি সংগ্রহ তৈরি করার প্রশ্নটি নিম্নরূপ:
>db.getSizeOfArray.insertOne({"StudentId":1,"StudentName":"Larry","StudentMarks":[87,34,5
6,77,89,90]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c6ebc536fd07954a4890680")
}
>db.getSizeOfArray.insertOne({"StudentId":2,"StudentName":"Sam","StudentMarks":[90,76,56
]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c6ebc6b6fd07954a4890681")
}
>db.getSizeOfArray.insertOne({"StudentId":3,"StudentName":"Carol","StudentMarks":[90,76]})
;
{
"acknowledged" : true,
"insertedId" : ObjectId("5c6ebc7a6fd07954a4890682")
} এখন আপনি find() পদ্ধতির সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করতে পারেন। প্রশ্নটি নিম্নরূপ:
> db.getSizeOfArray.find().pretty();
নিম্নলিখিত আউটপুট:
{
"_id" : ObjectId("5c6ebc536fd07954a4890680"),
"StudentId" : 1,
"StudentName" : "Larry",
"StudentMarks" : [
87,
34,
56,
77,
89,
90
]
}
{
"_id" : ObjectId("5c6ebc6b6fd07954a4890681"),
"StudentId" : 2,
"StudentName" : "Sam",
"StudentMarks" : [
90,
76,
56
]
}
{
"_id" : ObjectId("5c6ebc7a6fd07954a4890682"),
"StudentId" : 3,
"StudentName" : "Carol",
"StudentMarks" : [
90,
76
]
} একটি অ্যারের আইটেম সংখ্যা গণনা করার জন্য নিম্নলিখিত প্রশ্ন:
>db.getSizeOfArray.aggregate({$project:{NumberOfItemsInArray:{$size:"$StudentMarks"}}}).p
retty(); নিম্নলিখিত আউটপুট:
{ "_id" : ObjectId("5c6ebc536fd07954a4890680"), "NumberOfItemsInArray" : 6 }
{ "_id" : ObjectId("5c6ebc6b6fd07954a4890681"), "NumberOfItemsInArray" : 3 }
{ "_id" : ObjectId("5c6ebc7a6fd07954a4890682"), "NumberOfItemsInArray" : 2 }