Wednesday, January 18, 2017

Creating NEW collection TEST and inserting 20k records using FOR LOOP & LIMIT, SKIP & Sort combination OUTPUT

Creating NEW collection TEST and inserting 20k records using FOR LOOP

for(var i=0; i<20000; i++) {db.test.insert({x:i, y:"hi"});}

show collections

test

db.test.count()
20000


SKIP/LIMIT/SORT combination

db.test.find().limit(7)
db.test.find().skip(20).limit(5)
db.test.find().sort({x:-1}.skip(20).limit(5)

var query=db.test.find().sort({x:-1}.skip(20).limit(5)
query


Eg:

Write a query that retrieves documents of type "exam", sorted by score in descending order, skipping the first 50 and showing only the next 20.
db.scores.find({type:"exam"}).sort({score:-1}).skip(50).limit(20)

No comments:

Post a Comment