【问题标题】:Adding some data onto distinct query in mongodb在 mongodb 中将一些数据添加到不同的查询中
【发布时间】:2017-01-31 15:00:54
【问题描述】:

我有一个要聚合的集合,使特定字段与众不同,但还将其他字段数据添加到该聚合结果中。下面是我收藏的一个例子

   {
        "_id": ObjectId("57d6bc99e4b014fc13cf9579"),
        "_class": "hbservlet.FinalSubmissions",
        "active": true,
        "mappedTracks": 
        [
            { "position": 0, "title": "01. Ain't No Time.mp3", "url": "/published/music/tr157d6bc99e4b014fc13cf9579_.aac" }, 
            { "position": 1, "title": "02. In Her Mouth.mp3", "url": "/published/music/tr257d6bc99e4b014fc13cf9579_.aac" }, 
            { "position": 2, "title": "03. Maybach.mp3", "url": "/published/music/tr357d6bc99e4b014fc13cf9579_.aac" }
        ],
        "createdBy": ObjectId("57d6bb99b17ee01a5427af08"),
        "userId": ObjectId("57d6bb99b17ee01a5427af08"),
        "artistname": "test",
    }, {
        "_id": ObjectId("14d6bc99ebc942fc13cf9579"),
        "_class": "hbservlet.FinalSubmissions",
        "active": false,
        "mappedTracks": [
            { "position": 0, "title": "partysong.mp3", "url": "/published/music/tr114d6bc99ebc942fc13cf9579_.aac" }, 
            { "position": 1, "title": "outside", "url": "/published/music/tr214d6bc99ebc942fc13cf9579_.aac" },],
        "createdBy": ObjectId("57d6bb99b17ee01a5427af08"),
        "userId": ObjectId("57d6bb99b17ee01a5427af08"),
        "artistname": "mynameismyname",
    }

我使用不同的查询 (db.published.distinct("mappedTracks")) 来收集所有的 mappedTracks,所以我得到了这个

{ "position": 0, "title": "01. Ain't No Time.mp3", "url": "/published/music/tr157d6bc99e4b014fc13cf9579_.aac" }, 
{ "position": 1, "title": "02. In Her Mouth.mp3", "url": "/published/music/tr257d6bc99e4b014fc13cf9579_.aac" }, 
{ "position": 2, "title": "03. Maybach.mp3", "url": "/published/music/tr357d6bc99e4b014fc13cf9579_.aac" },
{ "position": 0, "title": "partysong.mp3", "url": "/published/music/tr114d6bc99ebc942fc13cf9579_.aac" }, 
{ "position": 1, "title": "outside", "url": "/published/music/tr214d6bc99ebc942fc13cf9579_.aac" }

这是我想要的结果,但我还想将它所属的文档的_id、用户ID、艺术家名添加到创建的新对象中。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您可以尝试MongoDB aggregation 运算符$unwind$group。例如:

    db.collection.aggregate([
              {$unwind:"$mappedTracks"}, 
              {$group:{_id:
                           {mappedTracks:"$mappedTracks", 
                            id:"$_id", 
                            userId:"$userId", 
                            artistname:"$artistname"
                            }
                       }
               }
    ])
    

    请注意,这是假设您现在想要所有这四个字段的不同值组合。如果您只想区分mappedTracks,您必须决定如何处理userIdartistname 的重复值。如果是这种情况,请参阅$first 运算符以在重复的情况下使用第一个值。

    如果您想在$group 之后重命名某些字段,另请参阅聚合运算符$project

    如果此查询是您用例的常用查询,我建议您重新考虑您的Data Modelling or Document Structure

    【讨论】:

    • 感谢 wan,这为我指明了正确的方向。我正在重新考虑我的文档结构以获得更高效的系统。
    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多