I have such collection structure:
{
_id: "JPsqqGJBgpwix5AqM",
images: [
{
id: 123456,
created: Thu Nov 14 2013 22:58:11 GMT+0200 (EET),
height: 115,
width: 350,
url: "http://www.test.com/alckxm.jpg"
},
{
id: 123456,
created: Thu Jan 24 2013 01:46:55 GMT+0200 (EET),
height: 115,
width: 350,
url: "http://www.test.com/awerrkxm.jpg"
}
],
username: "John"
},
...
What I need is to return all images from this collection, sorted by the date.
I've tried all of the following:
return Users.findOne({username: "John"}, {created: 1})
return Users.findOne({username: "John"}, {"images.created": 1})
return Users.findOne({username: "John"}, {sort: {created: 1}})
return Users.findOne({username: "John"}, {sort: {"images.created": 1}})
but nothing of this worked.
Is it even possible to do that now?
Sort is used only to sort whole documents. You'd need to do a findOne
and then sort the images array separately. For example:
var images = Users.findOne({username: "John"}).images;
return _.sortBy(images, function(image){ return image.created; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With