第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 MongoDB 的 Java 驅(qū)動(dòng)程序?qū)⒍鄠€(gè)文檔合并為一個(gè)文檔?

如何使用 MongoDB 的 Java 驅(qū)動(dòng)程序?qū)⒍鄠€(gè)文檔合并為一個(gè)文檔?

斯蒂芬大帝 2023-08-16 16:11:51
我正在處理包含音樂播放列表的文檔。每個(gè)文檔都有這樣的結(jié)構(gòu):{    "user_id": "5858",    "playlists": [        {            "name": "My Playlist",            "guild_ids": ["7575"],            "items": [                {                     "title": "title",                     "url": "url",                     "duration": 200000                }             ]        }    ]}我想提取同一公會(huì)的所有播放列表。但問題是我希望結(jié)果在單個(gè)文檔中返回。包含播放列表列表的一個(gè)文檔。guild_id=5656 的預(yù)期結(jié)果如下:{    "playlists": [        {            "name": "My Playlist",            "guild_ids": ["5656"],            "items": [                {                     "title": "title",                     "url": "url",                     "duration": 200000                }             ]        },        // other playlists where guild_ids contains "5656"    ]}我嘗試使用聚合,但我總是得到與唯一 user_ids 數(shù)量相同的文檔數(shù)量。我得到按 user_id 分組的播放列表。
查看完整描述

1 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

以下查詢可以獲得預(yù)期的輸出:


db.collection.aggregate([

    {

        $unwind:"$playlists"

    },

    {

        $match:{

            "playlists.guild_ids":{

                $in:["7575"]

            }

        }

    },

    {

        $group:{

            "_id":null,

            "playlists":{

               $push: "$playlists"

            }

        }

    },

    {

        $project:{

            "_id":0

        }

    }

]).pretty()

數(shù)據(jù)集:


{

  "_id" : ObjectId("5d88225e38db7cf8d3f75cd6"),

  "user_id" : "5858",

  "playlists" : [

    {

      "name" : "My Playlist",

      "guild_ids" : [

        "7575"

      ],

      "items" : [

        {

          "title" : "title",

          "url" : "url",

          "duration" : 200000

        }

      ]

    }

  ]

}

{

  "_id" : ObjectId("5d88225e38db7cf8d3f75cd7"),

  "user_id" : "5858",

  "playlists" : [

    {

      "name" : "My Playlist 2",

      "guild_ids" : [

        "1234"

      ],

      "items" : [

        {

          "title" : "title",

          "url" : "url",

          "duration" : 200000

        }

      ]

    }

  ]

}

{

  "_id" : ObjectId("5d88225e38db7cf8d3f75cd8"),

  "user_id" : "5858",

  "playlists" : [

    {

      "name" : "My Playlist 3",

      "guild_ids" : [

        "7575"

      ],

      "items" : [

        {

          "title" : "title",

          "url" : "url",

          "duration" : 200000

        }

      ]

    }

  ]

}

輸出:


{

    "playlists" : [

        {

            "name" : "My Playlist",

            "guild_ids" : [

                "7575"

            ],

            "items" : [

                {

                    "title" : "title",

                    "url" : "url",

                    "duration" : 200000

                }

            ]

        },

        {

            "name" : "My Playlist 3",

            "guild_ids" : [

                "7575"

            ],

            "items" : [

                {

                    "title" : "title",

                    "url" : "url",

                    "duration" : 200000

                }

            ]

        }

    ]

}

查詢分析:我們正在展開playlists,僅過濾具有7575公會(huì)ID的那些,然后將它們重新分組。


查看完整回答
反對(duì) 回復(fù) 2023-08-16
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)