Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

sort orderby array #2355

Unanswered
giduya asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 1 comment

Comment options

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...

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /