Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这有效:
return articles.find({}, {sort: {'published': -1}, limit: 1});
这不会:
sort_order = 'published'; return articles.find({}, {sort: {sort_order: -1}, limit: 1});
有没有办法做到这一点?
{}JSON在使用定义的形式定义时,不能将键值对中的第一个值作为变量。但是,您可以改用这种方式:
{}
var sort_order = {}; sort_order["published"] = -1; return articles.find({}, {sort: sort_order, limit: 1});
这应该让您最终查询:
return articles.find({}, {sort: {published:-1}, limit: 1});