FAQ

[mongodb-user] Bitwise operations while querying

Brahmana
Feb 4, 2012 at 7:03 pm
Hi,

If I have an integer field in my document which I want to use like a
bitmask flag. I read about the bit-wise operators for updating. Is
there something similar for querying?

Consider the following flags

TYPE1 = 0x01
TYPE2 = 0x02
TYPE3 = 0x04
TYPE4 = 0x08
TYPE5 = 0x16

A document can belong to multiple types. For such documents I would
"or" the types and store it in the "type" integer field.

Now if I want to query for all documents belonging to "TYPE1" a "bit-
wise and" operation has to be performed to find the matching
documents. Is there such an operator or do I have to provide a
Javascript string like this :

db.collection.find({"$where" : "this.type & 1"})

The doc at : http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand{{%24where}}
, says that this kind of JS in the query slows down execution and also
it cannot use an index.

In my case
- there will definitely be another criteria in the query like this :
db.collection.find({"user_id" : an_object_id, "$where" : "this.type &
1"})
- I will have an index on user_id
- The number of possible values the field "type" can have will very
likely not be more than 5.

Under these circumstances is JS in the query going to be that bad in
terms of performance?

Is there a better way to achieve this?

Regards,
Brahmana
@sribrahmana (http://www.twitter.com/sribrahmana)

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
reply

Search Discussions

2 responses

  • Barrie at Feb 6, 2012 at 7:58 pm
    Support for bitwise queries has been requested-- you can show your
    support for it by tracking the Jira ticket: https://jira.mongodb.org/browse/SERVER-3518
    On Feb 4, 2:02 pm, brahmana wrote:
    Hi,

    If I have an integer field in my document which I want to use like a
    bitmask flag. I read about the bit-wise operators for updating. Is
    there something similar for querying?

    Consider the following flags

    TYPE1 = 0x01
    TYPE2 = 0x02
    TYPE3 = 0x04
    TYPE4 = 0x08
    TYPE5 = 0x16

    A document can belong to multiple types. For such documents I would
    "or" the types and store it in the "type" integer field.

    Now if I want to query for all documents belonging to "TYPE1"  a "bit-
    wise and" operation has to be performed to find the matching
    documents. Is there such an operator or do I have to provide a
    Javascript string like this :

    db.collection.find({"$where" : "this.type & 1"})

    The doc at :http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...{{%24where}}
    , says that this kind of JS in the query slows down execution and also
    it cannot use an index.

    In my case
    - there will definitely be another criteria in the query like this :
    db.collection.find({"user_id" : an_object_id, "$where" : "this.type &
    1"})
    - I will have an index on user_id
    - The number of possible values the field "type" can have will very
    likely not be more than 5.

    Under these circumstances is JS in the query going to be that bad in
    terms of performance?

    Is there a better way to achieve this?

    Regards,
    Brahmana
    @sribrahmana (http://www.twitter.com/sribrahmana)
    --
    You received this message because you are subscribed to the Google Groups "mongodb-user" group.
    To post to this group, send email to mongodb-user@googlegroups.com.
    To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
  • Brahmana at Feb 7, 2012 at 4:39 am
    Thank you. I have voted on that JIRA ticket.

    So for now I guess I will have to go with the JS string with the
    $where.
    On Feb 7, 12:58 am, Barrie wrote:
    Support for bitwise queries has been requested-- you can show your
    support for it by tracking the Jira ticket:https://jira.mongodb.org/browse/SERVER-3518

    On Feb 4, 2:02 pm, brahmana wrote:






    Hi,
    If I have an integer field in my document which I want to use like a
    bitmask flag. I read about the bit-wise operators for updating. Is
    there something similar for querying?
    Consider the following flags
    TYPE1 = 0x01
    TYPE2 = 0x02
    TYPE3 = 0x04
    TYPE4 = 0x08
    TYPE5 = 0x16
    A document can belong to multiple types. For such documents I would
    "or" the types and store it in the "type" integer field.
    Now if I want to query for all documents belonging to "TYPE1"  a "bit-
    wise and" operation has to be performed to find the matching
    documents. Is there such an operator or do I have to provide a
    Javascript string like this :
    db.collection.find({"$where" : "this.type & 1"})
    The doc at :http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...{{%24where}}
    , says that this kind of JS in the query slows down execution and also
    it cannot use an index.
    In my case
    - there will definitely be another criteria in the query like this :
    db.collection.find({"user_id" : an_object_id, "$where" : "this.type &
    1"})
    - I will have an index on user_id
    - The number of possible values the field "type" can have will very
    likely not be more than 5.
    Under these circumstances is JS in the query going to be that bad in
    terms of performance?
    Is there a better way to achieve this?
    Regards,
    Brahmana
    @sribrahmana (http://www.twitter.com/sribrahmana)
    --
    You received this message because you are subscribed to the Google Groups "mongodb-user" group.
    To post to this group, send email to mongodb-user@googlegroups.com.
    To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Related Discussions

Discussion Navigation
viewthread | post

2 users in discussion

Brahmana: 2 posts Barrie: 1 post