技术点滴

mongodb sort string 字段,转化成numeric

关键是加入collation.并numericOrdering:true.

db.test.find().collation({"locale": "en", numericOrdering:true}).sort({k:1});

输出结果:
{ "_id" : ObjectId("5de9d990e8ff135a4297e179"), "n" : "null", "k" : null }
{ "_id" : ObjectId("5de9d99be8ff135a4297e17a"), "n" : "null exit" }
{ "_id" : ObjectId("5de9d9c5e8ff135a4297e17b"), "n" : "t3check", "k" : "3check" }
{ "_id" : ObjectId("5de9d980e8ff135a4297e178"), "n" : "t24", "k" : "24" }
{ "_id" : ObjectId("5de9d971e8ff135a4297e177"), "n" : "t123", "k" : "123" }


电话咨询
—— 售后咨询 ——
—— 商务合作 ——
微信咨询

售后咨询

商务咨询