Saturday 21 February 2015

mongoDB : $limit (aggregation)


$limit limits the number of documents passed to the next stage in the pipeline.

Syntax
{ $limit: <positive integer> }


> db.products.find()
{ "_id" : 1, "name" : "Nokia X3", "category" : "cellphone", "manufacturer" : "Nokia", "price" : 103 }
{ "_id" : 2, "name" : "Dell Inspiron i3147-3750", "category" : "laptop", "manufacturer" : "DELL", "price" : 359 }
{ "_id" : 3, "name" : "Nokia Lumia 521", "category" : "cellphone", "manufacturer" : "Nokia", "price" : 65 }
{ "_id" : 4, "name" : "Google Nexus 9 tablet", "category" : "tablet", "manufacturer" : "Google", "price" : 194 }
{ "_id" : 5, "name" : "Apple MacBook Air MD711LL/B", "category" : "laptop", "manufacturer" : "Apple", "price" : 854 }
{ "_id" : 6, "name" : "Apple MacBook Air MD760LL/B", "category" : "laptop", "manufacturer" : "Apple", "price" : 859 }
{ "_id" : 7, "name" : "Samsung Galaxy S III", "category" : "cellphone", "manufacturer" : "Samsung", "price" : 199 }
{ "_id" : 8, "name" : "Samsung Galaxy S5, White", "category" : "cellphone", "manufacturer" : "Samsung", "price" : 569 }
{ "_id" : 9, "name" : "Samsung Galaxy S2, White", "category" : "cellphone", "manufacturer" : "Samsung", "price" : 469 }
{ "_id" : 10, "name" : "Google Nexus 10 tablet", "category" : "tablet", "manufacturer" : "Google", "price" : 254 }
{ "_id" : 11, "name" : "Nokia x2", "category" : "cellphone", "manufacturer" : "Nokia", "price" : 93 }
{ "_id" : 12, "name" : "Apple iPhone 4", "category" : "cellphone", "manufacturer" : "Apple", "price" : 121 }
{ "_id" : 13, "name" : "Apple iPhone 5", "category" : "cellphone", "manufacturer" : "Apple", "price" : 409 }
{ "_id" : 14, "name" : "Apple iPhone 6", "category" : "cellphone", "manufacturer" : "Apple", "price" : 695 }
>
>
> db.products.aggregate([{"$project": {"id" : "$_id", "details" : {"manufacturer" : "$manufacturer", "category" : "$category"}}}])
{ "_id" : 1, "id" : 1, "details" : { "manufacturer" : "Nokia", "category" : "cellphone" } }
{ "_id" : 2, "id" : 2, "details" : { "manufacturer" : "DELL", "category" : "laptop" } }
{ "_id" : 3, "id" : 3, "details" : { "manufacturer" : "Nokia", "category" : "cellphone" } }
{ "_id" : 4, "id" : 4, "details" : { "manufacturer" : "Google", "category" : "tablet" } }
{ "_id" : 5, "id" : 5, "details" : { "manufacturer" : "Apple", "category" : "laptop" } }
{ "_id" : 6, "id" : 6, "details" : { "manufacturer" : "Apple", "category" : "laptop" } }
{ "_id" : 7, "id" : 7, "details" : { "manufacturer" : "Samsung", "category" : "cellphone" } }
{ "_id" : 8, "id" : 8, "details" : { "manufacturer" : "Samsung", "category" : "cellphone" } }
{ "_id" : 9, "id" : 9, "details" : { "manufacturer" : "Samsung", "category" : "cellphone" } }
{ "_id" : 10, "id" : 10, "details" : { "manufacturer" : "Google", "category" : "tablet" } }
{ "_id" : 11, "id" : 11, "details" : { "manufacturer" : "Nokia", "category" : "cellphone" } }
{ "_id" : 12, "id" : 12, "details" : { "manufacturer" : "Apple", "category" : "cellphone" } }
{ "_id" : 13, "id" : 13, "details" : { "manufacturer" : "Apple", "category" : "cellphone" } }
{ "_id" : 14, "id" : 14, "details" : { "manufacturer" : "Apple", "category" : "cellphone" } }


Skip first 5 documents

> db.products.aggregate([{"$project": {"id" : "$_id", "details" : {"manufacturer" : "$manufacturer", "category" : "$category"}}}, {$skip : 5}])
{ "_id" : 6, "id" : 6, "details" : { "manufacturer" : "Apple", "category" : "laptop" } }
{ "_id" : 7, "id" : 7, "details" : { "manufacturer" : "Samsung", "category" : "cellphone" } }
{ "_id" : 8, "id" : 8, "details" : { "manufacturer" : "Samsung", "category" : "cellphone" } }
{ "_id" : 9, "id" : 9, "details" : { "manufacturer" : "Samsung", "category" : "cellphone" } }
{ "_id" : 10, "id" : 10, "details" : { "manufacturer" : "Google", "category" : "tablet" } }
{ "_id" : 11, "id" : 11, "details" : { "manufacturer" : "Nokia", "category" : "cellphone" } }
{ "_id" : 12, "id" : 12, "details" : { "manufacturer" : "Apple", "category" : "cellphone" } }
{ "_id" : 13, "id" : 13, "details" : { "manufacturer" : "Apple", "category" : "cellphone" } }
{ "_id" : 14, "id" : 14, "details" : { "manufacturer" : "Apple", "category" : "cellphone" } }


Skip first 5 documents and limit result by 2 documents
> db.products.aggregate([{"$project": {"id" : "$_id", "details" : {"manufacturer" : "$manufacturer", "category" : "$category"}}}, {$skip : 5}, {"$limit":2}])
{ "_id" : 6, "id" : 6, "details" : { "manufacturer" : "Apple", "category" : "laptop" } }
{ "_id" : 7, "id" : 7, "details" : { "manufacturer" : "Samsung", "category" : "cellphone" } }




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment