Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a floor function in Mongodb aggregation framework?

I try to do some integer math in mongodb and have some problems. Is there a way to round doubles up or down in the mongo db aggregation framework?

like image 497
CasualT Avatar asked Nov 29 '12 01:11

CasualT


2 Answers

As per the comment from Stennie, the floor and ceiling methods are not supported.

And as per my comment above, the best solution that I could find was just doing an operation that looks like: (num-mod(num,1)) which should yield the same result as the floor function.

like image 109
CasualT Avatar answered Oct 08 '22 06:10

CasualT


Starting from MongoDB 3.2, you can use the following arithmetic aggregation operators are available:

  • ceil
  • trunc
  • floor

which allow to convert to integer from any side you want. The syntax is straight-forward: { $function: <number> }

like image 36
Salvador Dali Avatar answered Oct 08 '22 04:10

Salvador Dali