sql order by - How to sort results by string length on MongoDB -
i can on mysql
select * table order length(field) asc
how can on mongodb?
mongodb 3.4 introduces $strlencp
aggregation operator supports this. example:
db.collection.aggregate( [ {$project: { "field": 1, "field_length": { $strlencp: "$field" } }}, {$sort: {"field_length": -1}}, {$project: {"field_length": 0}}, ] )
Comments
Post a Comment