Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-42133

$regex does not match ObjectIds

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • ALL

      I have a collection having an ObjectId field named idConnectedobject and I want to find every document matching the required regex.

      The collection format :
       

      {{ "_id" : ObjectId("5beac9c68bc93e15056021a9"), "idConnectedobject" : ObjectId("5beac9bd8bc93e1505601c4b"), "idParameter" : ObjectId("e30020000000000000000061"), "value" : "RER", "entryDate" : ISODate("2018-11-13T12:55:30.357Z"), "__v" : 0}

      The non-working request :
       

      db.ConnectedobjectValue.aggregate([{ $match: { idConnectedobject: RegExp("5d133.*a7") } }])
      

      I've also tried :

      db.ConnectedobjectValue.find({ idConnectedobject: RegExp("5d133.*a7") })

       
      The working request :

      db.ConnectedobjectValue.aggregate([{ $project: { idConnectedobject: { $toString: '$idConnectedobject' } } }, { $match: { idConnectedobject: RegExp("5d133.*a7") } }])
      

       
      I have to cast the field i*dConnectedobject* to a String before I can apply the regex.

      My proposal to fix this is either that mongodb returns a warning or an error about using a regex on an ObjectId, or make the regex to work along ObjectId fields.

      PS: thank you for all your awesome work

      PS2: I've put this into Bogue but maybe it's a proposal Idk

      PS3: I've tried this in node.js using mongoose and also in mongo-cli on centos in mongodb version 4.0.4

       

       

            Assignee:
            eric.sedor@mongodb.com Eric Sedor
            Reporter:
            orelsanpls Grégory NEUT
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: