I have a mongoDB collection with 360,000 documents and a text file with 160,000 lines with id and text separated by dash like below format:
333-nice
66446-bad
88-good
...
I want when the id before dashes in the text file match with a field in the collection documents, Update or create a document in the new collection.
I used the following query but it is very slow and it takes a long time to do:
db.items_01.find().snapshot().forEach(function(elem)
{
var products = cat("/Users/amirali/Desktop/kala.txt");
var lines = products.split('\n');
for(var i = 0;i < lines.length;i++)
{
var g_name = lines[i].split("-").pop();
var pIg = g_name.replace("\"","");
var pId = lines[i].substr(1, lines[i].indexOf('-')-1);
var field = elem.i_code;
if(field.substring(0, 2) == pId)
{
db.items_additionals.update({
"i_code": field
},
{
$set:
{
"i_code" : field,
"class_id": field.substring(0, 2),
"class": pIg
}
},{upsert:true});
}
if(field.substring(0, 3) == pId)
{
db.items_additionals.update({
"i_code": field
},
{
$set:
{
"i_code" : field,
"subclass_id": field.substring(0, 3),
"subclass": pIg
}
},{upsert:true});
}
if(field.substring(0, 4) == pId)
{
db.items_additionals.update({
"i_code": field
},
{
$set:
{
"i_code" : field,
"group_id": field.substring(0, 4),
"group": pIg
}
},{upsert:true});
}
if(field.substring(0, 5) == pId)
{
db.items_additionals.update({
"i_code": field
},
{
$set:
{
"i_code" : field,
"subgroup_id": field.substring(0, 5),
"subgroup": pIg
}
},{upsert:true});
}
if(field.substring(0, 6) == pId)
{
db.items_additionals.update({
"i_code": field
},
{
$set:
{
"i_code" : field,
"category_id": field.substring(0, 6),
"category": pIg
}
},{upsert:true});
}
if(field.substring(0, 7) == pId)
{
db.items_additionals.update({
"i_code": field
},
{
$set:
{
"i_code" : field,
"subcategory_id": field.substring(0, 7),
"subcategory": pIg
}
},{upsert:true});
}
}
});
Notice: i_code field in documents is like 8816370532410001
How can I change this query for fast progressing?