mongoDB中按组嵌套的输出

mongoDB中按组嵌套的输出,mongodb,mongoose,Mongodb,Mongoose,我有一个这样的收藏 let Movies= [ {year: '2000', language: 'English', genre: 'Romance' , name: 'A beautiful day'}, {year: '2000', language: 'English', genre: 'Action' , name: 'A Dangerous day'}, {year: '2000', language: 'French', genre: 'Romance' , name: 'some

我有一个这样的收藏

let Movies= [
{year: '2000', language: 'English', genre: 'Romance' , name: 'A beautiful day'},
{year: '2000', language: 'English', genre: 'Action' , name: 'A Dangerous day'},
{year: '2000', language: 'French', genre: 'Romance' , name: 'someromancename'},
{year: '2000', language: 'French', genre: 'Action' , name: 'someactionname'},]
我需要得到一个组格式的输出,可能如下所示:

{
"2000" : {
            "English": {
                        "Romance": [{
                                    "name":"A beautiful day"
                                     //other fields
                                    }]
                        "Action": [{
                                    "name":"A dangerous day"
                                    }]
                        },
            "French": {
                        "Romance": [{
                                    "name":"Some Romance Name"
                                    }]
                        "Action": [{
                                    "name":"Some Action Name"
                                    }]
                        },
        }
}

我尝试过使用聚合,但无法获得准确的查询,因为我是MongoDB新手。这是我试图做的,但没有达到预期的结果

db.getCollection('Movies').aggregate([
{$group: {_id: {"year" : "$year",
            "language": "$language" , 
            "genre": "$genre" , 
            
            },
       "movies": {"$push": "$$ROOT"}
      } 
  },
  {$group: {_id: "$_id.year",
            "movies": {"$push": {
                "language": "$_id.language",
                "genre": "$_id.genre" , 
                "movies": "$movies"
                }}     
  }
  },

  ])
你可以试试

  • $group
    年份、语言和流派
    分组,并在
    电影
  • $group
    年份和语言
    分组,并以k和v格式制作具有流派的数组
  • $group
    进行,并在使用$arrayToObject将上述类型数组转换为对象后生成语言数组
  • 使用
    $arrayToObject
    将语言数组转换为对象,使用
    $arrayToObject
    将带有
    语言的年份转换为对象,并使用
    $replaceRoot
    将该对象替换为根对象
你可以试试

  • $group
    年份、语言和流派
    分组,并在
    电影
  • $group
    年份和语言
    分组,并以k和v格式制作具有流派的数组
  • $group
    进行,并在使用$arrayToObject将上述类型数组转换为对象后生成语言数组
  • 使用
    $arrayToObject
    将语言数组转换为对象,使用
    $arrayToObject
    将带有
    语言的年份转换为对象,并使用
    $replaceRoot
    将该对象替换为根对象

db.getCollection('Movies').aggregate([
  {
    $group: {
      _id: {
        "year": "$year",
        "language": "$language",
        "genre": "$genre"
      },
      "movies": { "$push": { name: "$name" } }
    }
  },
  {
    $group: {
      _id: {
        year: "$_id.year",
        language: "$_id.language"
      },
      "movies": {
        "$push": {
          k: "$_id.genre",
          v: "$movies"
        }
      }
    }
  },
  {
    $group: {
      _id: "$_id.year",
      movies: {
        $push: {
          k: "$_id.language",
          v: { $arrayToObject: "$movies" }
        }
      }
    }
  },
  {
    $replaceRoot: {
      newRoot: {
        $arrayToObject: [[
            {
              k: "$_id",
              v: { $arrayToObject: "$movies" }
            }
        ]]
      }
    }
  }
])