如何连接 mongodb 集合中拆分字段的两个元素?

How can I concat two elements of splitted fields from mongodb collections?

我在 mongodb 集合中有 github 个回购 URL。 (评论)

url: "https://api.github.com/repos/andymckay/solitude/comments/1573261" 

我想根据斜线(/)分隔符拆分该字段。 在此过程之后,我在下面有喜欢的数组。

https:, , api.github.com, repos, andymckay, solitude, .comments, 1573261

我想用斜杠运算符连接数组的第 4 个和第 5 个元素; 最后我想获得这样一个字段;

full_name :"andymckay/solitude"

我是 mongodb 的新手,我用这个查询解决了这个问题;

db.getCollection("commentsURL").aggregate([
       {$project: 
          {
             _id:1,
             url2:{$arrayElemAt:[{$split:["$url", "/"]}, 4]},
             url3:{$arrayElemAt:[{$split:["$url", "/"]}, 5]}
          }
       },
       { $addFields: { full_name: { $concat: [ "$url2", "/", "$url3" ] } } },      
       {$out:"comments2"}
     ]);

此查询的结果,它创建了下面的集合;

{ 
    "_id" : ObjectId("5266780cbd35436070000001"), 
    "url2" : "andymckay", 
    "url3" : "solitude", 
    "full_name" : "andymckay/solitude"
}

有没有比我更简单的方法?

db.getCollection("commentsURL").aggregate([
       {$project: 
          {
             _id:1,
             url2:{$arrayElemAt:[{$split:["$url", "/"]}, 4]},
             url3:{$arrayElemAt:[{$split:["$url", "/"]}, 5]}
          }
       },
       { $addFields: { full_name: { $concat: [ "$url2", "/", "$url3" ] } } },      
       {$out:"comments2"}
     ]);

经过投影查询,我可以得到 full_name 个 repos;

db.getCollection("comments").find({},{"_id":1,"full_name":1}).forEach(function(doc){
db.comments2.insert(doc);});