首页 > 解决方案 > 如何对文档中的数组进行排序?

问题描述

我正在尝试对子数组数据进行排序。以下是涉及的模型:

运营商型号:

const mongoose = require('mongoose');
var deepPopulate = require('mongoose-deep-populate')(mongoose);
const Schema = mongoose.Schema;
const OperatorSchema = new Schema({
    value: {type: String, required: true},
    displaySort: {type: Number, required: true }
})
OperatorSchema.plugin(deepPopulate);
module.exports = Operator = mongoose.model('operators', OperatorSchema)

运营模式:

const mongoose = require('mongoose');
var deepPopulate = require('mongoose-deep-populate')(mongoose);
const Schema = mongoose.Schema;
const OperativeSchema = new Schema({
    affiliation: {type: String, required: true},
    operator_REF: { type: Schema.Types.ObjectId, ref: 'operators' },
    number: {type: Number, required: true, default: 0},
})
OperativeSchema.plugin(deepPopulate);
module.exports = Operative = mongoose.model('operatives', OperativeSchema)

sFilter 型号:

const mongoose = require('mongoose');
var deepPopulate = require('mongoose-deep-populate')(mongoose);
const Schema = mongoose.Schema;
const FilterSchema = new Schema({

    affiliation: {type: String, required: true},
    name: {type: String, required: true},
    minOperative_REF: { type: Schema.Types.ObjectId, ref: 'operatives', required: false },
    maxOperative_REF: { type: Schema.Types.ObjectId, ref: 'operatives', required: false },
})
FilterSchema.plugin(deepPopulate);  // required for deepPopulate
module.exports = Filter = mongoose.model('sfilter', FilterSchema)

以下是对所有 sFilter 文档执行 GET 的代码:

Filter.find()
.deepPopulate(filter_deepPopulateString)
.then(fil => {
    if(fil.length > 0 ) {
        res.json(fil);
    } else {
        res.status(401).json({error: 'No filter found'});
    }
});

这是 filter_deepPopulateString :

'minOperative_REF.operator_REF maxOperative_REF.operator_REF'

这是“获取”结果:

[
    {
        "_id": "5d55907094315b13143b7623",
        "affiliation": "cvimmkfzukyn2qdrxumz",
        "name": "25 - 29",
        "__v": 0,
        "minOperative_REF": {
            "number": 25,
            "_id": "5d55916894315b13143b7628",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df7dc2c19253c3ebca2",
                "value": ">=",
                "displaySort": -20,
                "__v": 0
            },
            "__v": 0
        },
        "maxOperative_REF": {
            "number": 29,
            "_id": "5d55917194315b13143b7629",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df8dc2c19253c3ebca3",
                "value": "<=",
                "displaySort": 20,
                "__v": 0
            },
            "__v": 0
        }
    },
    {
        "_id": "5d55a9b86ad2d22f1cf7983c",
        "affiliation": "cvimmkfzukyn2qdrxumz",
        "name": "1 - 19",
        "__v": 0,
        "minOperative_REF": {
            "number": 1,
            "_id": "5d55913c94315b13143b7624",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df7dc2c19253c3ebca2",
                "value": ">=",
                "displaySort": -20,
                "__v": 0
            },
            "__v": 0
        },
        "maxOperative_REF": {
            "number": 19,
            "_id": "5d55914b94315b13143b7625",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df8dc2c19253c3ebca3",
                "value": "<=",
                "displaySort": 20,
                "__v": 0
            },
            "__v": 0
        }
    }
]

我想按以下数据排序:

"minOperative_REF": { "number": 25
"minOperative_REF": { "number": 1

这就是我想要的结果:

[
    {
        "_id": "5d55a9b86ad2d22f1cf7983c",
        "affiliation": "cvimmkfzukyn2qdrxumz",
        "name": "1 - 19",
        "__v": 0,
        "minOperative_REF": {
            "number": 1,
            "_id": "5d55913c94315b13143b7624",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df7dc2c19253c3ebca2",
                "value": ">=",
                "displaySort": -20,
                "__v": 0
            },
            "__v": 0
        },
        "maxOperative_REF": {
            "number": 19,
            "_id": "5d55914b94315b13143b7625",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df8dc2c19253c3ebca3",
                "value": "<=",
                "displaySort": 20,
                "__v": 0
            },
            "__v": 0
        }
    },
    {
        "_id": "5d55907094315b13143b7623",
        "affiliation": "cvimmkfzukyn2qdrxumz",
        "name": "25 - 29",
        "__v": 0,
        "minOperative_REF": {
            "number": 25,
            "_id": "5d55916894315b13143b7628",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df7dc2c19253c3ebca2",
                "value": ">=",
                "displaySort": -20,
                "__v": 0
            },
            "__v": 0
        },
        "maxOperative_REF": {
            "number": 29,
            "_id": "5d55917194315b13143b7629",
            "affiliation": "cvimmkfzukyn2qdrxumz",
            "operator_REF": {
                "_id": "5d531df8dc2c19253c3ebca3",
                "value": "<=",
                "displaySort": 20,
                "__v": 0
            },
            "__v": 0
        }
    }
]

这是我正在使用的 deepPopulate 插件:https ://www.npmjs.com/package/mongoose-deep-populate

标签: mongodbsorting

解决方案


据我所知,mongoDB 确实对文档进行排序,但不对响应文档内的数组中的对象进行排序(要清楚 - DB 可以使用数组的嵌套对象中的字段对返回的文档进行排序),因为您使用.findOne()的是您只会在响应中返回一个文档,这对您的排序阶段没有任何价值。如果您在使用时返回了多个文档,.find()那么 DB 肯定会根据嵌套对象的字段对文档进行排序,它仍然会与其他文档进行比较,但不会与返回的同一文档的同一数组中的其他对象进行比较。

如果您仍然需要它,请尝试执行您的代码或使用$aggregate来获取文档然后$unwind在该数组上使用,然后您可以对这些对象进行排序并再次推送回$group舞台上的同一数组,这样您仍然可以返回一个带有排序数组的文档。我不确定它是如何deepPopulate工作的,如果它类似于.populate猫鼬那么你可以使用$lookup(虽然只是一个提示)。


推荐阅读