我如何按字符串进行排序!null && exists >> bool is true >> createDate is从最新的到最旧的?
这是我尝试过的:
// -1 = descending/newest, right?
var sort =
{
"str" : -1, // str will either null, "" or "filled". I don't think this works.
"bool" : -1, // bool will be true or false. Seems to maybe work.
"createDate" : -1 // date obj. Seems to work.
};发布于 2018-08-18 21:16:59
正确答案是bool工作,createDate工作,但字符串不工作:
我发现bool和createDate过滤得很好--只是字符串做不到。我编写了一个脚本来遍历我所有的数据库以添加一个bool (如果str存在的话)(并添加到脚本中以备将来使用)。
因此:
// -1 = descending/newest
var sort =
{
"strExistsBool" : -1,
"bool" : -1,
"createDate" : -1
};然后使用:
db.collection.find(someQuery).sort(sort)https://stackoverflow.com/questions/51801705
复制相似问题