Is it possible to sort my MongoDB array of reviews by the date the review was made? Would I have to do this in my server-side script rather than by using a query operator? If so how would I do that?
{
"review": [
{
"date": "2012-05-04 21:06:58",
"review": "The Review",
"name": "The Persons Name",
},
{
"date": "2012-09-04 21:06:58",
"review": "The Review",
"name": "The Persons Name",
},
{
"date": "2012-02-04 21:06:58",
"review": "The Review",
"name": "The Persons Name",
}
],
"category": "Category 1",
"country": "USA",
"date": "2012-05-04 21:06:58"
}
Since you put php in the tags, I assume that you’re working with PHP. You can create your own sorting function for this and have it called by PHP
usort()function.The above code will sort the reviews by date in ascending order.