Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.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
Javascript 猫鼬填充排序无法正常工作_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 猫鼬填充排序无法正常工作

Javascript 猫鼬填充排序无法正常工作,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,当我调试这个问题时,我发现猫鼬排序不能正常工作。我删除了我们的代码并进行了简单的测试。你可以运行它,看看它会失败 也许以前有人见过?也许我错过了什么 谢谢你的帮助 var mongoose = require('mongoose'); var assert = require('assert'); mongoose.set('debug', true); mongoose.connect('mongodb://localhost/test'); var CarSchema = new mon

当我调试这个问题时,我发现猫鼬排序不能正常工作。我删除了我们的代码并进行了简单的测试。你可以运行它,看看它会失败

也许以前有人见过?也许我错过了什么

谢谢你的帮助

var mongoose = require('mongoose');
var assert = require('assert');

mongoose.set('debug', true);
mongoose.connect('mongodb://localhost/test');

var CarSchema = new mongoose.Schema({
    name: String
});
mongoose.model('Car', CarSchema);

var CarsSchema = new mongoose.Schema({
    car: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Car'
    },
    quantity: Number
});

var OrderSchema = new mongoose.Schema({
    suborders: [CarsSchema]
});
mongoose.model('Cars', OrderSchema);

var Car = mongoose.model('Car');
var Cars = mongoose.model('Cars');

Car.create([
    {
        name: 'Tesla'
    },
    {
        name: 'BMW'
    }
], function (err, objs) {

    Cars.create({
        suborders: [
            {
                car: objs[1]._id, //BMW
                quantity: 1
            }, {
                car: objs[0]._id, //Tesla
                quantity: 2
            }
        ]
    }, function (err, order) {

        Cars.findById(order._id)
            .populate({
                path: 'suborders.car',
                options: {
                    sort: '-name'
                }
            }).exec(function (err, cars) {

            assert.equal(cars.suborders[0].car.name, 'Tesla', 'DESC: 0 should be Tesla');
            assert.equal(cars.suborders[1].car.name, 'BMW', 'DESC: 1 should be BMW');

            assert.equal(cars.suborders[0].quantity, 2, 'DESC: Tesla quantity should be 2');
            assert.equal(cars.suborders[1].quantity, 1, 'DESC: BMW quantity should be 1');

        });
    });
});

不幸的是,您无法在Mongoose/mongodb中对填充的字段进行排序。事实上,由于猫鼬的存在,这里几乎存在着人口稠密的田野。但它们并不存在于实际的数据库中(因为mongo不支持连接),这就是实际应用排序的地方


因此,您必须使用javascript内置函数array.sort(compare)或任何您喜欢的必需模块手动对结果数组进行排序。

不幸的是,您无法对Mongoose/mongodb中填充的字段进行排序。事实上,由于猫鼬的存在,这里几乎存在着人口稠密的田野。但它们并不存在于实际的数据库中(因为mongo不支持连接),这就是实际应用排序的地方

因此,您必须使用javascript内置函数array.sort(compare)或任何您喜欢的必需模块手动对结果数组进行排序

我认为这可能会对你有所帮助,因为在猫鼬中填充选项 提供正确的排序顺序

我认为这可能会对你有所帮助,因为在猫鼬中填充选项 提供正确的排序顺序


这是一个确认的猫鼬bug,据说不容易修复(

关键现象是前两个断言通过,然后在第三个断言中失败


也许您可以更改模式设计或搜索类似“mongoose子文档排序”的内容。这是一个应用
聚合
的示例。这是一个已确认的mongoose错误,并且说不容易修复(

关键现象是前两个断言通过,然后在第三个断言中失败

也许您可以更改模式设计或搜索类似“mongoose子文档排序”的内容。这是一个应用
aggregate
的示例

  Cars.findById(order._id)
                .populate({
                    path: 'suborders.car',
                    options: {
                        sort: {name:-1}
                    }
                }).exec(function (err, cars) {
});