Reputation: 2703
Currently I use this code to search for the first possible 'slug' based on a string. It is recursive so, it's stops when he found a possible slug.
function determineProductSlug(name, count, callback){
var nameSlug = name;
if(count > 0){
nameSlug = name + '-' + count;
}
Product.find({'en.nameSlug': nameSlug}).sort({_id: -1}).exec(function(err, product){
if(err){
return callback(err);
}
if(product.length == 0){
return callback(null, nameSlug);
}else{
count = count+1;
return determineProductSlug(name, count, callback);
}
})
}
But, it is getting very slow. We have now arround 10.000 documents in the collection, but that will be many more.
How can I speed this this function/query to operate faster? I have tried it wit the sort({id: -1})
but that doesn't seems to help.
Upvotes: 0
Views: 364
Reputation: 370
If you have the rights to do it, one way to improve performances is to create an index on en.nameSlug. Indexes are binary trees kept in memory, that would be the fastest way to find. Indexes can slow down write performances though but you would know if the most important is read or write.
Upvotes: 1