|
Hi bond,
We actually are working on implementing type conversion expressions in the expression language as part of the 3.7.x development release series. In fact, commit 3fe0f3a062397 under SERVER-32784 (pushed to the master branch yesterday) adds support for a $convert expression that supports numerical typecasts. You should now be able to do something like this:
> db.c.drop()
|
true
|
> db.c.insert({num: NumberInt(3)})
|
WriteResult({ "nInserted" : 1 })
|
> db.c.insert({num: NumberLong(4)})
|
WriteResult({ "nInserted" : 1 })
|
> db.c.insert({num: 5.2})
|
WriteResult({ "nInserted" : 1 })
|
> db.c.aggregate([{$project: {out: {$convert: {input: "$num", to: "double"}}}}])
|
{ "_id" : ObjectId("5a871d1d5cf7d58f48c8a43c"), "out" : 3 }
|
{ "_id" : ObjectId("5a871d225cf7d58f48c8a43d"), "out" : 4 }
|
{ "_id" : ObjectId("5a871d2a5cf7d58f48c8a43e"), "out" : 5.2 }
|
> db.c.aggregate([{$project: {out: {$convert: {input: "$num", to: "int"}}}}])
|
{ "_id" : ObjectId("5a871d1d5cf7d58f48c8a43c"), "out" : 3 }
|
{ "_id" : ObjectId("5a871d225cf7d58f48c8a43d"), "out" : 4 }
|
{ "_id" : ObjectId("5a871d2a5cf7d58f48c8a43e"), "out" : 5 }
|
Since $convert and related features are still under development, we don't have documentation to point you to yet. The documentation should be complete as release candidates for the stable release containing $convert become available.
I'm going to resolve this as a duplicate of SERVER-32784. In the meantime, let me know if you have any questions.
Best,
Dave
|