Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major - P3 Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.9.1
    • Component/s: Querying
    • Labels:
      None
    • Backport:
      No
    • # Replies:
      30
    • Last comment by Customer:
      true

      Description

      In addition to the $or operator (http://jira.mongodb.org/browse/SERVER-205), I feel there is a need for a $and operator.
      A possible way to write it could be:

      $and: [

      { <condition1> }

      ,

      { <condition2> }

      ]

      Why it is needed:
      Condition blocks are physically objects. Thus, we cannot have the same property used twice, preventing the use of the same construct/property multiple times in a query.

      Example:
      Suppose I have a collection of user sessions.
      Each session can contain an arbitrary list of key-value pairs, possibly repeated (ie. an identical key can be listed several times in different objects of the list). Additionally, each key value pair has a timestamp, for history purpose.

      A document sample would be
      {
      session_id: 1,
      data: [

      { name: 'johm', timestamp: 20100504120000 }

      ,

      { lastname: 'doe', timestamp: 20100505120000 }

      ,

      { name: 'john', #the user corrected the initial input timestamp: 20100504120100 }

      ]
      }

      Now, when I want to query for a session with an user named john, I would need to use $elemMatch, since I can't match the full element because of the variable timestamp, like below:

      find({
      data: {$elemMatch: {name: 'john'}}
      });

      Now, I want to look for john doe. But since I can't repeat the same $elemMatch key in the data condition object, I am stuck.
      The query below would run, but ignore the first condition, since it is being overwritten by the second.

      find({
      data: {
      $elemMatch:

      {name: 'john'}

      ,
      $elemMatch:

      {lastname: 'doe'}

      }
      });

      Possible solution with $and operator:

      find({
      data: {
      $and: [
      {$elemMatch: {name: 'john'}},
      {$elemMatch: {lastname: 'doe'}}
      ]
      }
      });

        Issue Links

          Activity

          Hide
          Ed Rooth
          added a comment -

          +1 for Mikael Nousiainen's comment.
          Nested $or is crucial.

          Show
          Ed Rooth
          added a comment - +1 for Mikael Nousiainen's comment. Nested $or is crucial.
          Hide
          Andy Jefferson
          added a comment -

          +1 for nested $or/$and combinations.

          Show
          Andy Jefferson
          added a comment - +1 for nested $or/$and combinations.
          Hide
          Aaron Staple (Inactive)
          added a comment -

          Hi Folks,

          Thanks for letting us know of your interest in nesting $or within $and.

          Support for nesting $or within $and is a nontrivial task, and it requires that we implement SERVER-2585 first.

          I've filed SERVER-3192 to specifically cover nesting $or within $and, so you can track the issue there.

          Thanks,
          Aaron

          Show
          Aaron Staple (Inactive)
          added a comment - Hi Folks, Thanks for letting us know of your interest in nesting $or within $and. Support for nesting $or within $and is a nontrivial task, and it requires that we implement SERVER-2585 first. I've filed SERVER-3192 to specifically cover nesting $or within $and, so you can track the issue there. Thanks, Aaron
          Hide
          auto
          added a comment -

          Author:

          {u'login': u'astaple', u'name': u'Aaron', u'email': u'aaron@10gen.com'}

          Message: SERVER-1089 add some checks for multiple where fields
          Branch: master
          https://github.com/mongodb/mongo/commit/36a3b7aed3470677944ebf75ee8d018ead897d72

          Show
          auto
          added a comment - Author: {u'login': u'astaple', u'name': u'Aaron', u'email': u'aaron@10gen.com'} Message: SERVER-1089 add some checks for multiple where fields Branch: master https://github.com/mongodb/mongo/commit/36a3b7aed3470677944ebf75ee8d018ead897d72
          Hide
          Antoine Girbal
          added a comment -

          it could also be useful to be able to do ranges in the $in query.
          This would be equivalent to a nested implicit $or on a single key.
          Typically smthing like {prop: {$in: [1,2,3]}} gets converted into single value ranges to look at in index.
          So one could write smthing like:
          db.foo.find({"bar" : {$in: [

          { "$gte" : 012 , "$lt" : 014}

          ,

          { "$gte" :012}

          ]}})
          Dont know if that is planned or if there would be complications, but would make that kind of query easy and efficient.

          Show
          Antoine Girbal
          added a comment - it could also be useful to be able to do ranges in the $in query. This would be equivalent to a nested implicit $or on a single key. Typically smthing like {prop: {$in: [1,2,3] }} gets converted into single value ranges to look at in index. So one could write smthing like: db.foo.find({"bar" : {$in: [ { "$gte" : 012 , "$lt" : 014} , { "$gte" :012} ]}}) Dont know if that is planned or if there would be complications, but would make that kind of query easy and efficient.

            People

            • Votes:
              59 Vote for this issue
              Watchers:
              41 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                2 years, 43 weeks, 2 days ago
                Date of 1st Reply: