silviud
silviud

Reputation: 1025

Mongodb query modulo with dynamic field

I have a collection with entries as

{_id" : ObjectId("52ff99402cfff2733d826bfe"),"f" : 60}

I want to make a query where the "f" field will be incremented with a random value and passed on modulo operator - something like

db.col.find( { f + RANDOM  : { $mod : [100,0] } })

This obviously is not working since the filter has to be done on a field name for collection. How can it be done ?

thanks

Upvotes: 1

Views: 773

Answers (1)

Ivan.Srb
Ivan.Srb

Reputation: 1851

You could use $where:

db.col.find( { $where: "(this.f + RANDOM) % 100 == 0" } )

or:

db.col.find( function() { return ((this.f + RANDOM) % 100 == 0 ) } );

But this solution have some constraints, you could read about it there: http://docs.mongodb.org/manual/reference/operator/query/where/

Upvotes: 1

Related Questions