[feature request? bug?] $mod should works on ObjectID too

$gt, $lt works with ObjectID; I don’t see why $mod is unsupported.
The use case is common: sharding multiple jobs to be executed on multiple task runners.

Is this a bug, a feature request, or something in between?
How do I proceed with the request? Does this forum where it is supposed to be?

On second thought, I can also implement the functionality at the ODM layer.
By adding a field populated with randomized integers.

But the question still stands; basically, shouldn’t anything that works with integers also works on ObjectIDs too?

Hi @3Ji,

What’s the actual command you’re attempting to execute with $mod? This might give some clarity in regards to whether or not this should be a feature request or not. Please include some sample ObjectID values too if possible.

The $mod (aggregation) documentation states:

The arguments can be any valid expression as long as they resolve to numbers.


I’ll try with Mongoid syntax.
How about this:

  _id: {
    '$mod': [4, 0],
}).each do |record|

Where Job is Mongoid::Document and _id is a field of ObjectID.

BTW, I don’t use aggregation in my projects, so I referred to $mod (query) in all of my contexts.

Thanks for confirming - An error will be returned if the divisor or remainder values evaluate to:

  • NaN (not a number) or Infinity.
  • A value that cannot be represented using a 64-bit integer.

In terms of your post / question - Are you wanting to specify ObjectId() values where the 4 and 0 exist? That is, the divisor and remainder are ObjectId() values.

If this is the case, then perhaps raising a feedback post for this which include your use case details would be the right path forward.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.