I have been working on a simple function to calculate prime numbers using a NoSQL database. Despite trying different approaches, I kept encountering an error stating that the value I was searching for is not defined. Any feedback or suggestions would be greatly appreciated.
Here's what I have attempted:
- First, I created a set of random numbers:
for (var i = 0; i < 1000; i++) db.exemplary.insert( {x: Math.random()} );
2. Next, I defined a map function
map = function() {
for (var i = 2; i < x; i++){
if(x % i == 0)
try {} catch (e) {};
}
emit(i, value);
};
- Then, I created the reduce function:
reduce = function (i,value){return array.sum(value)}
- After assigning the collection to a variable, I called the function:
collection = db.exemplary
var result = collection.mapReduce(map, reduce, {out: {inline: 1}});
Based on my basic understanding, the map step should verify if a number is prime or not, while the reduce step should add up all instances of prime numbers. The error I encountered mentioned that x is not defined, even though it is present in the db.exemplary collection.
I am open to any suggestions for improving the functionality described above.
EDIT: At the moment, my function is not "emitting" any results from the map operation. I am currently addressing this issue.