Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS+;MongoDB排序查询_Node.js_Mongodb - Fatal编程技术网

Node.js NodeJS+;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

我试图用node和mongodb按字母顺序排序。这是我目前的状况,但不起作用:

$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}
    }
}

你能从你的收藏中添加一个示例文档吗?再添加一些代码。对!很抱歉,添加了更多信息。如果需要更多的信息,请告诉我