-
Notifications
You must be signed in to change notification settings - Fork 1.4k
-
This are my documents:
{
"_id": "621113c8166924399446756e",
"metadata":
{
"country": "mexico",
"lenguage": "spanish",
"continent": "america",
"demographics":
[
"population": 3,000,000,
"minority" : "white people"
],
[
"population": 5,000,000,
"minority" : "indigenous people"
],
[
"population": 500,000,
"minority" : "black people"
],
}
how can i order by population:
$pagination = Countries::orderBy('continent'.'asc')->orderby('country','asc')->orderby('demographics.population','asc');
i get this error:
Executor error during find command :: caused by :: cannot sort with keys that are parallel arrays
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Hi Giduya,
Well the message tells you the issue "cannot sort with keys that are parallel arrays" demographics should be an array of objects and you should use project & unwind in aggregate.
You data should looks like :
{
"_id": {
"$oid": "62b02d3c0dcd9192dfcd3f29"
},
"metadata": {
"country": "mexico",
"lenguage": "spanish",
"continent": "america"
},
"demographics": [
{
"population": 3000000,
"minority": "white people"
},
{
"population": 5000000,
"minority": "indigenous people"
},
{
"population": 500000,
"minority": "black people"
}
]
}
and your aggregate :
db.DB.aggregate([{ "$match" : { 'metadata.continent': 'america' }},{"$project" : {demographics : 1 }}, { "$unwind" : "$demographics" }, { "$sort" : { "demographics.population" : -1 }}]);
Apply this in you PHP framework...
Beta Was this translation helpful? Give feedback.