Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string to a number in MongoDB projection

I have a collection of documents that have a value that is known to be a number, but is stored as a string. It is out of my control to change the type of the field, but I want to use that field in an aggregation (say, to average it).

It seems that I should be using a projection prior to grouping, and in that projection convert the field as needed. I can't seem to get the syntax just right - everything I try either gives me NaN, or the new field is simply missing from the next step in the aggregation.

$project: {
    value: '$value',
    valueasnumber: ????
}

Given the very simple example above, where the contents of $value in all documents are string type, but will parse to a number, what do I do to make valueasnumber a new (non-existing) field that is of type double with the parsed version of $value in it?

I've tried things like the examples below (and about a dozen similar things):

{ $add: new Number('$value').valueOf() }
new Number('$value').valueOf()

Am I barking up the wrong tree entirely? Any help would be greatly appreciated!

(To be 100% clear, below is how I would like to use the new field).

$group {
    score: {
        $avg: '$valueasnumber'
    }
}
like image 844
Jesse English Avatar asked Sep 22 '14 21:09

Jesse English


People also ask

What does Projection do in MongoDB?

MongoDB Projection is a special feature allowing you to select only the necessary data rather than selecting the whole set of data from the document. For Example, If a Document contains 10 fields and only 5 fields are to be shown the same can be achieved using the Projections.

What is $$ in MongoDB?

To access the value of the variable, prefix the variable name with double dollar signs ( $$ ); i.e. "$$<variable>" . If the variable references an object, to access a specific field in the object, use the dot notation; i.e. "$$<variable>.

How do I change the datatype of a column in MongoDB?

You can change the data type of a field by using the data type selectors on the right of the field in the Atlas Cloud Cluster as well as MongoDB Compass . If you want to update it using Mongo shell or any specific drivers of MongoDB then you can refer to the $convert operator.

What is $Set in MongoDB?

$set outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields. Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.


1 Answers

One of the way which I can think of is to use a mongo shell javascript to modify the document by adding new number field, valuesasnumber (number conversion of existing string 'value' field) in the existing document or in the new doc. Then using this numeric field for further calculations.

db.numbertest.find().forEach(function(doc) {
    doc.valueasnumber = new NumberInt(doc.value);
    db.numbertest.save(doc);
});

Using the valueasnumber field for numeric calculation

db.numbertest.aggregate([{$group : 
   {_id : null, 
    "score" : {$avg : "$valueasnumber"}
   }
}]);
like image 86
Vijay Rawat Avatar answered Oct 11 '22 09:10

Vijay Rawat