我可以在mysql上轻松完成
select * from TABLE order by length(FIELD) asc
我怎样才能在 MongoDB 上做到这一点?
MongoDB 3.4 引入了$strLenCP https://docs.mongodb.com/manual/reference/operator/aggregation/strLenCP/最终支持这一点的聚合运算符。一个例子:
db.collection.aggregate(
[
{$project: {
"field": 1,
"field_length": { $strLenCP: "$field" }
}},
{$sort: {"field_length": -1}},
{$project: {"field_length": 0}}
]
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)