Reputation: 19
I try to sort a collection:
var assortiment_producten = db.get('assortiment_producten');
assortiment_producten.find({
product_ID: product_ID
}, {
sort: {
subcategorie_ID: 1
}
}, function(err, docs) {
});
But the received docs are not sorted by subcategorie_ID
Do I miss something here???
Upvotes: 1
Views: 3537
Reputation: 3242
According to the documentation on find()
, the first argument to find is a filter, and the second one is a projection. Hence your sort syntax will be interpreted as a projection, which is not what you want. For sorting, you need to use the sort()
function.
The following works for me:
> db.assortiment_producten.insert({ product_ID : 1, subcategorie_ID : 'A' })
> db.assortiment_producten.insert({ product_ID : 2, subcategorie_ID : 'B' })
> db.assortiment_producten.insert({ product_ID : 0, subcategorie_ID : 'C' })
> db.assortiment_producten.insert({ product_ID : 0, subcategorie_ID : 'D' })
> db.assortiment_producten.insert({ product_ID : 0, subcategorie_ID : 'A' })
> var product_ID = 0
> db.assortiment_producten.find({ product_ID : product_ID }).sort({ subcategorie_ID : 1 })
{ "_id" : ObjectId("5298b024de2b8992960081a0"), "product_ID" : 0, "subcategorie_ID" : "A" }
{ "_id" : ObjectId("5298b001de2b89929600819e"), "product_ID" : 0, "subcategorie_ID" : "C" }
{ "_id" : ObjectId("5298b01fde2b89929600819f"), "product_ID" : 0, "subcategorie_ID" : "D" }
Upvotes: 2