首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子文档数组中的级联字段

子文档数组中的级联字段
EN

Stack Overflow用户
提问于 2017-05-30 04:51:26
回答 1查看 741关注 0票数 1

我试图获得一组结果,玩家的名字用字符串格式分隔,但我似乎无法让它起作用。我可以让players数组连接每个子文档的名字和姓氏,但是将它们连接到一个字符串中是失败的。

这就是我目前的结果:

代码语言:javascript
复制
{ "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "name" : "Mary Mack" }, { "name" : "Mary Minor" } ], "net" : 4, "team" : 2 }
{ "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "name" : "Jane Doe" }, { "name" : "Julie Doe" } ], "net" : 3, "team" : 3 }
{ "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "name" : "Melanie Maygonuts" }, { "name" : "Mackenzie Mightbecray" } ], "net" : 3, "team" : 4 }
{ "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "name" : "Isabella Iamluny" }, { "name" : "Alexis Alreadythere" } ], "net" : 3, "team" : 5 }

这就是我想要的结果:

代码语言:javascript
复制
{"event" : "Junior Girls 16s", "field" : "Main", "team" : "Mary Mack / Mary Minor", "net" : 4, "team" : 2 }
{ "event" : "Junior Girls 16s", "field" : "Main", "team" : "Jane Doe / Julie Doe", "net" : 3, "team" : 3 }
{ "event" : "Junior Girls 16s", "field" : "Main", "team" : "Melanie Maygonuts / Mackenzie Mightbecray", "net" : 3, "team" : 4 }
{ "event" : "Junior Girls 16s", "field" : "Main", "team" : "Isabella Iamluny / Alexis Alreadythere", "net" : 3, "team" : 5 }

第一组结果的代码:

代码语言:javascript
复制
db.registrations.aggregate([
     {$match: {event: "Junior Girls 16s"}},
     {$project: {event: "$event", field: "$field", net: "$net", team: "$team",
      "players": {
              "$map": {
                  "input": "$players",
                  "as": "u",
                    "in": {
                        "name": { "$concat" : [ "$$u.first", " ", "$$u.last" ] }
                   }
              }
         } }}
 ])

这是我上一次尝试的代码,它给了我一个错误,无效操作符还原:

代码语言:javascript
复制
db.registrations.aggregate([         
    {$match: {event: "Junior Girls 16s"}},         
    {$project: {event: "$event", field: "$field", net: "$net", team: "$team",          
        "players": {                  
            "$map": {                      
                "input": "$players",                      
                "as": "u",                        
                "in": {                            
                    "name": { "$concat" : [ "$$u.first", " ", "$$u.last" ] }                       
                }    
            }
        } 
    }},
    {$project: {event: "$event", field: "$field", net: "$net", team: "$team",
        "players": {
          $reduce: {
            input: "$players",
            initialValue: "",
            in: { $concat: ['$$name', ' / ', '$$this'] }
          }
        }
    }}
    ])

我遗漏了什么?我觉得我离得太近了,只是无法按我想要的方式操纵数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-30 06:23:22

这实际上不应该通过聚合框架来完成,但是无论如何我们还是要展示这个示例。

如前所述,您的具体错误是因为您没有支持MongoDB的$reduce,这意味着MongoDB 3.4或更高。但是,在进行升级时,可以执行如下语句:

代码语言:javascript
复制
db.registrations.aggregate([
  { "$addFields": {
    "players": {
      "$reduce": {
        "input": { "$filter": {
          "input": { 
            "$reduce": {
              "input": { "$zip": {
                "inputs": [
                  { "$map": {
                    "input": "$players",
                    "as": "u",
                    "in": { "$concat": [ "$$u.first", " ", "$$u.last" ] }
                  }},
                  { "$map": {
                    "input": {
                      "$range": [ 0, { "$subtract": [ { "$size": "$players" }, 1 ] } ]
                    },
                    "as": "el",
                    "in": " / "
                  }}
                ],
                "useLongestLength": true
              }},
              "initialValue": [],
              "in": { "$concatArrays": [ "$$value", "$$this" ] }
            }
          },
          "as": "el",
          "cond": { "$ne": [ "$$el", null ] }
        }},
        "initialValue": "",
        "in": { "$concat": [ "$$value", "$$this" ] }
      }
    }
  }}
])

它将产生所需的输出:

代码语言:javascript
复制
{
    "_id" : ObjectId("592cfbc3820a42bc1e825de7"),
    "event" : "Junior Girls 16s",
    "field" : "Main",
    "players" : "Mary Mack / Mary Minor",
    "net" : 4,
    "team" : 2
}
{
    "_id" : ObjectId("592cfbc3820a42bc1e825de8"),
    "event" : "Junior Girls 16s",
    "field" : "Main",
    "players" : "Jane Doe / Julie Doe",
    "net" : 3,
    "team" : 3
}
{
    "_id" : ObjectId("592cfbc3820a42bc1e825de9"),
    "event" : "Junior Girls 16s",
    "field" : "Main",
    "players" : "Melanie Maygonuts / Mackenzie Mightbecray",
    "net" : 3,
    "team" : 4
}
{
    "_id" : ObjectId("592cfbc3820a42bc1e825dea"),
    "event" : "Junior Girls 16s",
    "field" : "Main",
    "players" : "Isabella Iamluny / Alexis Alreadythere",
    "net" : 3,
    "team" : 5
}

真是一口又一口的嚼,不是吗?这就是为什么您不会这样做,而只是在读取数据的客户端代码中编写这样的转换。

作为shell的一个简单的JavaScript示例:

代码语言:javascript
复制
db.registrations.find().forEach( doc => {
  doc.players = doc.players
    .map( p => `${p.first} ${p.last}` )
    .join(" / ");
  printjson(doc)
})

输出完全相同的东西。看看现在有多干净。

源数据

请注意,这个使用过的源数据在原始数组内容中对于数组中的名称具有“先”和“最后”字段的问题中进行了描述:

代码语言:javascript
复制
{ "_id" : ObjectId("592cfbc3820a42bc1e825de7"), "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "first" : "Mary", "last" : "Mack" }, { "first" : "Mary", "last" : "Minor" } ], "net" : 4, "team" : 2 }
{ "_id" : ObjectId("592cfbc3820a42bc1e825de8"), "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "first" : "Jane", "last" : "Doe" }, { "first" : "Julie", "last" : "Doe" } ], "net" : 3, "team" : 3 }
{ "_id" : ObjectId("592cfbc3820a42bc1e825de9"), "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "first" : "Melanie", "last" : "Maygonuts" }, { "first" : "Mackenzie", "last" : "Mightbecray" } ], "net" : 3, "team" : 4 }
{ "_id" : ObjectId("592cfbc3820a42bc1e825dea"), "event" : "Junior Girls 16s", "field" : "Main", "players" : [ { "first" : "Isabella", "last" : "Iamluny" }, { "first" : "Alexis", "last" : "Alreadythere" } ], "net" : 3, "team" : 5 }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44253671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档