Mongo 带排序的不同查询不起作用
Mongo distinct query with sort not working
我 运行 在 mongo 中使用 php 进行查询,如下所示:
$column ='address.roadname';
(new MongoDB\Client())->db->mycollection->distinct($column,[],['$sort'=> [$column => 1]]);
查询给出结果,但未排序。
我错过了什么?
数据只是“嵌套”到内部对象中的字符串
我期望的是按字母顺序排序的街道名称列表
示例数据:
[
{
"name": "tizio",
"address": [
{
"roadType": "via",
"roadname": "Roma",
"number": "12 bis",
"city": "Milano"
},
{
"roadType": "via",
"roadname": "Emilia",
"number": "124",
"city": "Modena"
},
{
"roadType": "via",
"roadname": "Appia",
"number": "89",
"city": "Genova"
}
]
},
{
"name": "caio",
"address": [
{
"roadType": "vicolo",
"roadname": "stretto",
"number": "12",
"town": "Monza"
},
{
"roadType": "largo",
"roadname": "Garibaldi",
"number": "24",
"city": "Modena"
},
{
"roadType": "piazza",
"roadname": "Armi",
"number": "26",
"city": "Rovigo"
}
]
},
{
"name": "sempronio",
"address": [
{
"roadname": "Roma",
"number": "15",
"city": "Milano"
},
{
"roadType": "via",
"roadname": "Po",
"number": "4",
"city": "Torino"
},
{
"roadType": "largo",
"roadname": "Garibaldi",
"number": "9",
"community": "Genova"
}
]
}
]
我的期望:
Appia,Armi,Emilia,Garibaldi,Po,Roma,Stretto
注意:如果我 运行 它在 mongo 控制台上
db.mycollection.distinct("address.roadname").sort()
我得到了预期的结果
distinct 的 PHP 实现确实有一个 sort
选项,参见 https://docs.mongodb.com/php-library/v1.7/reference/method/MongoDBCollection-distinct/
在您的 shell 示例中,
db.mycollection.distinct("address.roadname")
returns 一个数组,所以当你 运行 .sort()
时,它正在使用 javascript Array.sort 方法。
PHPdistinct函数也是returns一个数组,所以用sort函数就可以了。
我 运行 在 mongo 中使用 php 进行查询,如下所示:
$column ='address.roadname';
(new MongoDB\Client())->db->mycollection->distinct($column,[],['$sort'=> [$column => 1]]);
查询给出结果,但未排序。
我错过了什么?
数据只是“嵌套”到内部对象中的字符串
我期望的是按字母顺序排序的街道名称列表
示例数据:
[
{
"name": "tizio",
"address": [
{
"roadType": "via",
"roadname": "Roma",
"number": "12 bis",
"city": "Milano"
},
{
"roadType": "via",
"roadname": "Emilia",
"number": "124",
"city": "Modena"
},
{
"roadType": "via",
"roadname": "Appia",
"number": "89",
"city": "Genova"
}
]
},
{
"name": "caio",
"address": [
{
"roadType": "vicolo",
"roadname": "stretto",
"number": "12",
"town": "Monza"
},
{
"roadType": "largo",
"roadname": "Garibaldi",
"number": "24",
"city": "Modena"
},
{
"roadType": "piazza",
"roadname": "Armi",
"number": "26",
"city": "Rovigo"
}
]
},
{
"name": "sempronio",
"address": [
{
"roadname": "Roma",
"number": "15",
"city": "Milano"
},
{
"roadType": "via",
"roadname": "Po",
"number": "4",
"city": "Torino"
},
{
"roadType": "largo",
"roadname": "Garibaldi",
"number": "9",
"community": "Genova"
}
]
}
]
我的期望:
Appia,Armi,Emilia,Garibaldi,Po,Roma,Stretto
注意:如果我 运行 它在 mongo 控制台上
db.mycollection.distinct("address.roadname").sort()
我得到了预期的结果
distinct 的 PHP 实现确实有一个 sort
选项,参见 https://docs.mongodb.com/php-library/v1.7/reference/method/MongoDBCollection-distinct/
在您的 shell 示例中,
db.mycollection.distinct("address.roadname")
returns 一个数组,所以当你 运行 .sort()
时,它正在使用 javascript Array.sort 方法。
PHPdistinct函数也是returns一个数组,所以用sort函数就可以了。