Node.js NodeJS+;MongoDB排序查询
我试图用node和mongodb按字母顺序排序。这是我目前的状况,但不起作用:Node.js NodeJS+;MongoDB排序查询,node.js,mongodb,Node.js,Mongodb,我试图用node和mongodb按字母顺序排序。这是我目前的状况,但不起作用: $addToSet: { friends: { $each: [{ _id: req.body.globalUserId.toString(), username: req.body.globalUserName, language: req.body.globalUserLanguage, pro
$addToSet: {
friends: {
$each: [{
_id: req.body.globalUserId.toString(),
username: req.body.globalUserName,
language: req.body.globalUserLanguage,
profilePicture: req.body.globalUserProfilePicture
}],
$sort: {username: 1}
}
}
以下是一个示例文档:
正如您在friends数组下看到的,它先是“r1”,然后是“a”。因为我希望这个数组按字母顺序排序,所以“a”会排在第一位。我该怎么做?非常感谢
以下是我对该特定模型的模式:
username: String,
email: String,
password: String,
language: { type: String, default: "English" },
profilePicture: { type: String, default: "/images/talk/blank-profile-picture.png" },
pendingFriends: [this],
friends: [this]
集合是无序的,出于同样的原因,
$sort
不受支持
$push
将维持订单
$push: {
friends: {
$each: [{
_id: req.body.globalUserId.toString(),
username: req.body.globalUserName,
language: req.body.globalUserLanguage,
profilePicture: req.body.globalUserProfilePicture
}],
$sort: {username: 1}
}
}
你能从你的收藏中添加一个示例文档吗?再添加一些代码。对!很抱歉,添加了更多信息。如果需要更多的信息,请告诉我