If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
तो अभी तक हम collections से सभी या तो सभी documents fetch कर रहे थे या सिर्फ single document fetch करते थे। क्या हो अगर हमें n
documents चाहिए हो मतलब 10 , 12 या 15 ? या फिर किसी particular index
से किसी particular index तक documents चहिये हों ?
Well , उसके लिए MongoDB ने हमें limit()
और skip()
का concept दिया है , जिसकी help से हम need के according documents fetch कर सकते हैं।
और इस topic में हम limit()
और skip()
को अच्छे से समझेंगे ।
●●●
limit()
method MongoDB में एक query method है जो एक collection से data retrieve करते वक्त use होता है। ये method specify करता है कि आप कितने documents को retrieve करना चाहते हैं।
इस method का use किसी specific query के results को limit
करने के लिए किया जाता है , जिससे आप database से only required documents ही retrieve करते हैं और performance को improve कर सकते हैं।
db.collectionName.find().limit(number)
यहां, collectionName
आपके collection का नाम है जिसमे से data retrieve करना चाहते हैं , number
एक integer value है जो specify करता है कि कितने documents retrieve होंगे ,
Suppose आपके पास एक "employees"
collection है जिसमे employees की details stored है -
अब अगर आपको सिर्फ 2
employees की details retrieve करनी है , जिसकी query कुछ इस तरह से होगी।
db.employees.find().limit(2)
यह SQL limit की तरह है। जैसे ऊपर दी गयी MongoDB query की SQL equivalent query कुछ इस तरह से होगी -
SELECT * FROM employees LIMIT 2;
●●●
आप चाहे तो documents को filter करके भी limit()
apply कर सकते हैं -
db.employees.find({"name" : "Raju"}).limit(2)
इसी तरह से आप other conditionals को भी apply कर सकते हैं , और documents को delete / update भी कर सकते हैं।
●●●
skip()
method भी MongoDB में query method है जो limit()
method के साथ work करता है। skip()
method का use किसी collection से documents retrieve करते वक्त ये specify करता है कि आप कितने documents को skip
करके retrieve करना चाहते हैं।
db.collectionName.find().skip(number)
यहां , collectionName
collection का नाम है जिसमे से data retrieve करना है और number
एक integer value है जो specify करता है कि कितने documents को skip
करके documents fetch होंगे।
skip()
method का use ज्यादातर paging (pages में data retrieve करना ) और pagination (large datasets को multiple pages में display करना) scenarios में होता है।
इसके साथ limit()
method का use करके आप specific range के documents को retrieve कर सकते हैं।
Suppose आपके पास एक "students"
collection है जिसमे students की details stored है -
db.students.find().limit(2).skip(0)
SQL equivalent query
SELECT * FROM students LIMIT 2 OFFSET 0; // or SELECT * FROM students LIMIT 0,2;
इस query से first 2 students की details मिलेगी , अब अगर आपको next 2 students की details retrieve करनी है तो आप skip(2)
का use करेंगे और limit same ही रहेगी।
db.students.find().limit(2).skip(2)
●●●
हालाँकि aggregate() में भी records को fetch करने के लिए $limit
stage का use किया जाता है।
Suppose आपको students collection से 5 documents को fetch करना है , तो $limit
की query कुछ इस तरह से होगी।
db.students.aggregate([ { $limit : 5 } ]);
तो कुछ इस तरह से pagination काम करती है , हालाँकि limit
और skip
का number कम या ज्यादा हो सकता है। Normally pagination में limit 10 रहता है और skip dynamic होता है page number के according .
●●●
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook