【发布时间】:2015-07-15 21:57:44
【问题描述】:
我正在使用 MongoDB 和 Node.js 在页面中显示记录集。我已经按字母顺序在页面上显示它们,但我想在顶部显示一行(“默认”行),在其下方按字母顺序显示所有其他行。
我知道,我知道,Mongo 绝对不是 SQL,但在 SQL 中我会做这样的事情:
SELECT *
FROM themes
ORDER BY name != "Default", name ASC;
甚至可能
SELECT * FROM themes WHERE name = "Default"
UNION
SELECT * FROM themes WHERE name != "Default" ORDER BY name ASC;
我尝试了几种 Mongo 排序选项的变体,例如
"$orderby": {'name': {'$eq': 'Default'}, 'name': 1}}
但到目前为止没有任何运气。我一直在寻找解决这个问题的方法,但我没有找到任何东西。我是 Mongo 的新手,但也许我做错了。
我目前的基本代码:
var db = req.db;
var collection = db.get('themes');
collection.find({"$query": {}, "$orderby": {'name': 1}}, function(e, results) {
res.render('themes-saved', {
title: 'Themes',
section: 'themes',
page: 'saved',
themes: results
});
});
【问题讨论】:
标签: node.js mongodb mongodb-query