Variable in regex in MongoDB [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0
















This question already has an answer here:




  • Converting user input string to regular expression

    10 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 '18 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.

























    0
















    This question already has an answer here:




    • Converting user input string to regular expression

      10 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 '18 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.





















      0












      0








      0









      This question already has an answer here:




      • Converting user input string to regular expression

        10 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

        10 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

        10 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 '18 at 7:44









      golubluegolublue

      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 '18 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 '18 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.


























          0






          active

          oldest

          votes

















          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word