Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use bitwise operation for query in mongodb

I need to make query like this in MongoDB:

db.collection.find( { $where : "(this.type & some_num) ^ some_num == 0"} ) 

Some_num is a variable and it can be any integer.

I read the doc at here which says Javascript query slows down execution and also it cannot use an index and requires a table scan.

What is the alternative to the above query?

like image 653
Sujit Maharjan Avatar asked Oct 20 '22 20:10

Sujit Maharjan


2 Answers

The best thing to do in Mongo is to store the calculated value that you plan to search on in a field and then create an index on it. Otherwise, you are pretty much doomed to scanning the whole collection.

like image 138
Zeki Avatar answered Oct 23 '22 17:10

Zeki


$where must be slow.

It would be better something like $bitand, but it is not implemented yet. see https://jira.mongodb.org/browse/SERVER-3518.

Possible solution (for me) was using arrays.

like image 29
vvkatwss vvkatwss Avatar answered Oct 23 '22 17:10

vvkatwss vvkatwss