我在mongodb集合中有github repos 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"}
]);
在那次投影查询之后,我可以获得仓库的全名;
db.getCollection("comments").find({},{"_id":1,"full_name":1}).forEach(function(doc){
db.comments2.insert(doc);});