Variable in regex in MongoDB [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Converting user input string to regular expression

    9 answers



  • how to pass a variable to a regex

    4 answers




This works fine:



db.collecton.find({code: {
$regex: /babcb/,
$options: 'i'
}})


But how to do this with variable code = 'abc'? I tried to do like this, but it finds nothing:



db.collection.find({code: {
$regex: 'b' + code + 'b',
$options: 'i'
}})


(I need to find documents with field code equal the variable, but with /i option, so I can't write just code: code. With $regex: code it also returns documents with code like qabcg)










share|improve this question













marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 7:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    0
    down vote

    favorite













    This question already has an answer here:




    • Converting user input string to regular expression

      9 answers



    • how to pass a variable to a regex

      4 answers




    This works fine:



    db.collecton.find({code: {
    $regex: /babcb/,
    $options: 'i'
    }})


    But how to do this with variable code = 'abc'? I tried to do like this, but it finds nothing:



    db.collection.find({code: {
    $regex: 'b' + code + 'b',
    $options: 'i'
    }})


    (I need to find documents with field code equal the variable, but with /i option, so I can't write just code: code. With $regex: code it also returns documents with code like qabcg)










    share|improve this question













    marked as duplicate by Neil Lunn mongodb
    Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 22 at 7:49


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:




      • Converting user input string to regular expression

        9 answers



      • how to pass a variable to a regex

        4 answers




      This works fine:



      db.collecton.find({code: {
      $regex: /babcb/,
      $options: 'i'
      }})


      But how to do this with variable code = 'abc'? I tried to do like this, but it finds nothing:



      db.collection.find({code: {
      $regex: 'b' + code + 'b',
      $options: 'i'
      }})


      (I need to find documents with field code equal the variable, but with /i option, so I can't write just code: code. With $regex: code it also returns documents with code like qabcg)










      share|improve this question














      This question already has an answer here:




      • Converting user input string to regular expression

        9 answers



      • how to pass a variable to a regex

        4 answers




      This works fine:



      db.collecton.find({code: {
      $regex: /babcb/,
      $options: 'i'
      }})


      But how to do this with variable code = 'abc'? I tried to do like this, but it finds nothing:



      db.collection.find({code: {
      $regex: 'b' + code + 'b',
      $options: 'i'
      }})


      (I need to find documents with field code equal the variable, but with /i option, so I can't write just code: code. With $regex: code it also returns documents with code like qabcg)





      This question already has an answer here:




      • Converting user input string to regular expression

        9 answers



      • how to pass a variable to a regex

        4 answers








      mongodb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 7:44









      golublue

      11




      11




      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 at 7:49


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 at 7:49


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.































          active

          oldest

          votes






















          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          Sphinx de Gizeh

          Dijon

          Get global maximum slope