Using a custom function that returns the number of decimal places in a number:
var decimalPlaces = function (num) {
var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) { return 0; }
return Math.max(
0,
// Number of digits right of decimal point.
(match[1] ? match[1].length : 0)
// Adjust for scientific notation.
- (match[2] ? +match[2] : 0));
}
you can iterate over your collection using the forEach
method on the find()
cursor, save to another collection those documents which have the field value
decimal places greater than 2:
db.collection.find().forEach( function (x) {
if decimalPlaces(x.value) > 2
db.new_collection.save(x);
});