I have github repos URLs in a mongodb collection. (comments)
url: "https://api.github.com/repos/andymckay/solitude/comments/1573261"
I want to split this field according to slash (/) delimiter. After this process, I have array likes below.
https:, , api.github.com, repos, andymckay, solitude, .comments, 1573261
Than I want to concat 4. and 5. elements of array with slash operator; Finally I want to obtain a field like this;
full_name :"andymckay/solitude"
I am newby about mongodb, I solve this with this query;
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"}
]);
Results of this query, It creates the collection below;
{
"_id" : ObjectId("5266780cbd35436070000001"),
"url2" : "andymckay",
"url3" : "solitude",
"full_name" : "andymckay/solitude"
}
Is there any method easier than mine?
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"}
]);
After that a projection query, I can get full_name of repos;
db.getCollection("comments").find({},{"_id":1,"full_name":1}).forEach(function(doc){
db.comments2.insert(doc);});