Mongodb聚合嵌入式文档

时间:2018-01-04 18:47:47

标签: mongodb mongodb-query

我在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"
            }
        }
    }}
]}}}

我需要按每张卡的数量排序,所以我尝试使用聚合来做到这一点:

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

首先我使用unwind来分隔卡片组,然后我按照每张卡片的数量对元素进行排序,但是效果不好,它给了我:

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

如何正确完成?

0 个答案:

没有答案