Saturday, 21 February 2015

mongoDB : $sort (aggregation)


$sort sorts all input documents and returns them to the pipeline in sorted order.

Syntax
{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }

Aggregation framework supports disk and memory based sorting. The $sort stage has a limit of 100 megabytes of RAM. By default, if the stage exceeds this limit, $sort will produce an error. To allow for the handling of large datasets, set the allowDiskUse option to true to enable $sort operations to write to temporary files.

> 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 }


Without using $sort 
> db.products.aggregate([{"$group":{"_id": {"manufacturer":"$manufacturer","category": "$category", "price" : "$price"}}}])
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 695 } }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 93 } }
{ "_id" : { "manufacturer" : "Google", "category" : "tablet", "price" : 254 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 199 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 409 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 121 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "laptop", "price" : 859 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 569 } }
{ "_id" : { "manufacturer" : "Google", "category" : "tablet", "price" : 194 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 469 } }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 65 } }
{ "_id" : { "manufacturer" : "DELL", "category" : "laptop", "price" : 359 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "laptop", "price" : 854 } }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 103 } }


With $sort
> db.products.aggregate([{"$group":{"_id": {"manufacturer":"$manufacturer","category": "$category", "price" : "$price"}}}, {"$sort": {"_id.price":1}}])
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 65 } }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 93 } }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 103 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 121 } }
{ "_id" : { "manufacturer" : "Google", "category" : "tablet", "price" : 194 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 199 } }
{ "_id" : { "manufacturer" : "Google", "category" : "tablet", "price" : 254 } }
{ "_id" : { "manufacturer" : "DELL", "category" : "laptop", "price" : 359 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 409 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 469 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 569 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 695 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "laptop", "price" : 854 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "laptop", "price" : 859 } }


Using allowDiskUse option
> db.products.aggregate([{"$group":{"_id": {"manufacturer":"$manufacturer","category": "$category", "price" : "$price"}}}, {"$sort": {"_id.price":1}}], {"allowDiskUse":true})
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 65 } }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 93 } }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone", "price" : 103 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 121 } }
{ "_id" : { "manufacturer" : "Google", "category" : "tablet", "price" : 194 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 199 } }
{ "_id" : { "manufacturer" : "Google", "category" : "tablet", "price" : 254 } }
{ "_id" : { "manufacturer" : "DELL", "category" : "laptop", "price" : 359 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 409 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 469 } }
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone", "price" : 569 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "cellphone", "price" : 695 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "laptop", "price" : 854 } }
{ "_id" : { "manufacturer" : "Apple", "category" : "laptop", "price" : 859 } }
>



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment