Suppose I have some objects in MongoDB:
{
"_id":xxx,
"name":"mike",
"children": [
{"name":"A", "age":3},
{"name":"B", "age": 5}
]
}
If I want to get this "mike" with his children sorted by "age desc", what should I do?
I've looked at Mongoid(in rails), and morphia(in Java), not found the answer.
I don't know of any way to do this. You'll probably want to sort the children in code (Ruby, Java) when they come back.
This is one of the typical limitations of Mongo, you don't really "sort" the sub-objects on the server. Instead you pull them from the DB and then sort the sub-objects as necessary.
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