কার্সারের সাহায্যে সংগ্রহের মাধ্যমে লুপ করার সিনট্যাক্স নিচে দেওয়া হল
var anyVariableName1;
var anyVariableName2= db.yourCollectionName.find();
while(yourVariableName2.hasNext()) {
yourVariableName1= yourVariableName2.next(); printjson(yourVariableName1);
}; আসুন নথি সহ একটি সংগ্রহ তৈরি করি। নিম্নোক্ত প্রশ্নটি
> db.loopThroughCollectionDemo.insertOne({"StudentName":"John","StudentAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ca81f2d6669774125247f")
}
> db.loopThroughCollectionDemo.insertOne({"StudentName":"Larry","StudentAge":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ca8272d66697741252480")
}
> db.loopThroughCollectionDemo.insertOne({"StudentName":"Chris","StudentAge":25});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ca8462d66697741252481")
}
> db.loopThroughCollectionDemo.insertOne({"StudentName":"Robert","StudentAge":24});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9ca8632d66697741252482")
} Find() পদ্ধতি
এর সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করার জন্য নিম্নলিখিত প্রশ্ন রয়েছে> db.loopThroughCollectionDemo.find().pretty();
এটি নিম্নলিখিত আউটপুট তৈরি করবে
{
"_id" : ObjectId("5c9ca81f2d6669774125247f"),
"StudentName" : "John",
"StudentAge" : 23
}
{
"_id" : ObjectId("5c9ca8272d66697741252480"),
"StudentName" : "Larry",
"StudentAge" : 21
}
{
"_id" : ObjectId("5c9ca8462d66697741252481"),
"StudentName" : "Chris",
"StudentAge" : 25
}
{
"_id" : ObjectId("5c9ca8632d66697741252482"),
"StudentName" : "Robert",
"StudentAge" : 24
} কার্সারের সাহায্যে সংগ্রহগুলি লুপ করার জন্য নিম্নোক্ত ক্যোয়ারী
> var allDocumentValue;
> var collectionName=db.loopThroughCollectionDemo.find();
> while(collectionName.hasNext()){allDocumentValue= collectionName.next();printjson(allDocumentValue);
... } এটি নিম্নলিখিত আউটপুট তৈরি করবে
{
"_id" : ObjectId("5c9ca81f2d6669774125247f"),
"StudentName" : "John",
"StudentAge" : 23
}
{
"_id" : ObjectId("5c9ca8272d66697741252480"),
"StudentName" : "Larry",
"StudentAge" : 21
}
{
"_id" : ObjectId("5c9ca8462d66697741252481"),
"StudentName" : "Chris",
"StudentAge" : 25
}
{
"_id" : ObjectId("5c9ca8632d66697741252482"),
"StudentName" : "Robert",
"StudentAge" : 24
}