Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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
Arrays mongodb中的排序子数组_Arrays_Mongodb - Fatal编程技术网

Arrays mongodb中的排序子数组

Arrays mongodb中的排序子数组,arrays,mongodb,Arrays,Mongodb,我试图用子数组的值对MongoDb中的文档进行排序,但我没有得到我想要的 例如: 输入JSON: [{ _id : 512, Categories : [ { _id : 1, Ranking : 3, Type : 'Property', Value : 'Value 1' }, { _id : 12,

我试图用子数组的值对MongoDb中的文档进行排序,但我没有得到我想要的

例如: 输入JSON:

[{
    _id : 512,
    Categories : 
    [
        {
            _id : 1,
            Ranking : 3,
            Type : 'Property',
            Value : 'Value 1'
        },
        {
            _id : 12,
            Ranking : 2,
            Type : 'Argument',
            Value : 'Value 2'
        }
    ]
},
{
    _id : 100,
    Categories : 
    [
        {
            _id : 4,
            Ranking : 2,
            Type : 'Property',
            Value : 'Value 21'
        }
    ]
},
{
    _id : 122,
    Categories : 
    [
        {
            _id : 31,
            Ranking : 1,
            Type : 'Argument',
            Value : 'Value 1'
        },
        {
            _id : 12,
            Ranking : 1,
            Type : 'Property',
            Value : 'Value 12'
        }
    ]
}
]
我想用类型est属性所在的排名字段对元素进行排序

最终结果:

[
{
    _id : 122,
    Categories : 
    [
        {
            _id : 31,
            Ranking : 1,
            Type : 'Argument',
            Value : 'Value 1'
        },
        {
            _id : 12,
            Ranking : 1,
            Type : 'Property',
            Value : 'Value 12'
        }
    ]
},
{
    _id : 100,
    Categories : 
    [
        {
            _id : 4,
            Ranking : 2,
            Type : 'Property',
            Value : 'Value 21'
        }
    ]
}
{
    _id : 512,
    Categories : 
    [
        {
            _id : 1,
            Ranking : 3,
            Type : 'Property',
            Value : 'Value 1'
        },
        {
            _id : 12,
            Ranking : 2,
            Type : 'Argument',
            Value : 'Value 2'
        }
    ]
}
]
我试着这样做:

db.Collection.aggregate(
     { $match: {
        'Categories.Type' : 'Property'
    }},
    { $sort: {
        'Categories.Ranking' : 1
    }}
)
-->这给了我错误的顺序的结果

我也试过:

db.Collection.aggregate(
     { $unwind: '$Categories' },
     { $match: {
        'Categories.Type' : 'Property'
    }},
    { $sort: {
        'Categories.Ranking' : 1
    }}
)
-->最终结果是确定的,但我需要在结果中包含参数类型的其他字段

有人能帮忙吗


谢谢

如果文档有两个数组元素,其类型为“property”,该怎么办?在这种情况下,如何使用
排序
?使用较低的排名?更高的排名?在我的情况下,我不能有两种具有相同值的类型。