想象一下像这样的Mongo系列:
[
{
"roleName":"slave",
"id":2,
"__uc__roleName":"SLAVE",
"_children":{
"managers":[
]
},
"_id":ObjectId("5508e3e5875cbdcc712feda6"),
"_clean":true
},
{
"__uc__roleName":"BOSS",
"_children":{
"managers":[
{
"name":"Tony",
"surname":"Mobily",
"roleId":0,
"id":0,
"__uc__name":"TONY",
"__uc__surname":"MOBILY",
"_children":{
}
}
]
},
"_clean":true,
"_id":ObjectId("5508e3a9875cbdcc712feda4"),
"id":0,
"roleName":"boss"
},
{
"__uc__roleName":"EMPLOYEE",
"_children":{
"managers":[
{
"name":"Chiara",
"surname":"Mobily",
"roleId":1,
"id":1,
"__uc__name":"CHIARA",
"__uc__surname":"MOBILY",
"_children":{
}
},
{
"name":"Sara",
"surname":"Fabbietti",
"roleId":1,
"id":2,
"__uc__name":"SARA",
"__uc__surname":"FABBIETTI",
"_children":{
}
},
{
"name":"Daniela",
"surname":"Mobily",
"roleId":1,
"id":3,
"__uc__name":"DANIELA",
"__uc__surname":"MOBILY",
"_children":{
}
}
]
},
"_clean":true,
"_id":ObjectId("5508e3e0875cbdcc712feda5"),
"id":1,
"roleName":"employee"
}
]
此查询的预期结果是什么:
db.roles.find().sort( {'_children.managers.name': 1 } )
实际结果是:
1)奴隶
2)员工
3)Boss
但是......为什么?如果您按照具有多个元素的数组中的键进行排序,排序实际上如何工作?
答案 0 :(得分:2)
通过考虑在数组中每个项目中进行的字段引用的值,并考虑其与其他文档相比较的值,升序排序的工作方式应该完全正确。
当您考虑每个文档的匹配值时,排序可能是最佳可视化的,按顺序排列:
{ "roleName": "SLAVE", "_children.managers.name": null },
{ "roleName": "BOSS", "_children.managers.name":"Tony" },
{ "roleName": "EMPLOYEE", "_children.managers.name":"Chiara" }
那是因为那将是"最少" (升序)每个文档中该字段的有序值。
在" _children.managers.name"上排序时按升序排列的文件结果如下:
{ "roleName": "SLAVE", "_children.managers.name": null },
{ "roleName": "EMPLOYEE", "_children.managers.name":"Chiara" }
{ "roleName": "BOSS", "_children.managers.name":"Tony" },
你是如何看待它的。
所以"命令"文档值的多键输入,以及与其他文档相对应的文档的每个值。