0

I have the following database in mongodb:

{
 "notebook": 
  {"Year20162017": 
    {"Cards": [
      {"Card": {
        "Number": 1,
        "Player": {
            "Code": "a",
            "Team": {
                "Name": "ABC"
            }
        }
    }},
    {"Card": {
        "Number": 18,
        "Player": {
            "Code": "c",
            "Team": {
                "Name": "GHI"
            }
        }
    }},
    {"Card": {
        "Number": 35,
        "Player": {
            "Code": "b",
            "Team": {
                "Name": "DEF"
            }
        }
    }}
]}}}

I need to sort by the number of each card, so I tryed to do it using aggregations:

db.cromos1617.aggregate([
{$unwind: {"notebook.Year20162017.Cards"}},
{$sort: {"notebook.Year20172017.Cards.Card.Number": 1}}
])

First I used unwind to separate the array of Cards, then I sorted the elements by the number of each card, but it didn't worked well, it gave me:

2018-01-04T18:44:09.593+0000 E QUERY    [thread1] SyntaxError: missing : after property id @(shell):2:41

How it is done properly?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Paulo
  • 81
  • 2
  • 8
  • 1
    Couple of things incorrect here. `$unwind` takes the string reference($) and for string reference no document({}) is allowed. Try `db.cromos1617.aggregate([ {$unwind: "$notebook.Year20162017.Cards"}, {$sort: {"notebook.Year20172017.Cards.Card.Number": 1}} ])` – s7vr Jan 04 '18 at 18:51
  • It didn't worked, I changed the numbers randomly and when I apllied that it returned me the original database disorderly – Paulo Jan 04 '18 at 19:29
  • Seems typo in sort stage. it should be Year20162017 – s7vr Jan 04 '18 at 19:32
  • yeah it's working right, thanks – Paulo Jan 04 '18 at 19:34
  • Possible duplicate of [how to sort array inside collection record in mongoDB](https://stackoverflow.com/questions/13449874/how-to-sort-array-inside-collection-record-in-mongodb) – s7vr Jan 04 '18 at 19:40

0 Answers0