how to use variable as a field value in mongodb query? [duplicate]










1
















This question already has an answer here:



  • How do you use a variable in a regular expression?

    18 answers



how to use search_key variable as field value instead of static value BRO



if(search_key!='')

dbo.collection("assets").aggregate([

"$match": $and: [ status: 1 , $or: [ maker_name : /^.*BRO.*$/i , serial_number : /^.*BRO.*$/i ] ]
,

$lookup:

from: 'asset_type',
localField: 'asset_type',
foreignField: 'asset_type_id',
as: 'asset_type_details'


]).sort(_id:-1).toArray(function(err, result)
if (err) throw err;
res.status(200).json('return_data': result );
db.close();
);











share|improve this question















marked as duplicate by Neil Lunn node.js
Users with the  node.js badge can single-handedly close node.js 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 14 '18 at 21:13


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.






















    1
















    This question already has an answer here:



    • How do you use a variable in a regular expression?

      18 answers



    how to use search_key variable as field value instead of static value BRO



    if(search_key!='')

    dbo.collection("assets").aggregate([

    "$match": $and: [ status: 1 , $or: [ maker_name : /^.*BRO.*$/i , serial_number : /^.*BRO.*$/i ] ]
    ,

    $lookup:

    from: 'asset_type',
    localField: 'asset_type',
    foreignField: 'asset_type_id',
    as: 'asset_type_details'


    ]).sort(_id:-1).toArray(function(err, result)
    if (err) throw err;
    res.status(200).json('return_data': result );
    db.close();
    );











    share|improve this question















    marked as duplicate by Neil Lunn node.js
    Users with the  node.js badge can single-handedly close node.js 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 14 '18 at 21:13


    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.




















      1












      1








      1









      This question already has an answer here:



      • How do you use a variable in a regular expression?

        18 answers



      how to use search_key variable as field value instead of static value BRO



      if(search_key!='')

      dbo.collection("assets").aggregate([

      "$match": $and: [ status: 1 , $or: [ maker_name : /^.*BRO.*$/i , serial_number : /^.*BRO.*$/i ] ]
      ,

      $lookup:

      from: 'asset_type',
      localField: 'asset_type',
      foreignField: 'asset_type_id',
      as: 'asset_type_details'


      ]).sort(_id:-1).toArray(function(err, result)
      if (err) throw err;
      res.status(200).json('return_data': result );
      db.close();
      );











      share|improve this question

















      This question already has an answer here:



      • How do you use a variable in a regular expression?

        18 answers



      how to use search_key variable as field value instead of static value BRO



      if(search_key!='')

      dbo.collection("assets").aggregate([

      "$match": $and: [ status: 1 , $or: [ maker_name : /^.*BRO.*$/i , serial_number : /^.*BRO.*$/i ] ]
      ,

      $lookup:

      from: 'asset_type',
      localField: 'asset_type',
      foreignField: 'asset_type_id',
      as: 'asset_type_details'


      ]).sort(_id:-1).toArray(function(err, result)
      if (err) throw err;
      res.status(200).json('return_data': result );
      db.close();
      );






      This question already has an answer here:



      • How do you use a variable in a regular expression?

        18 answers







      node.js mongodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 12:13









      Saurabh Mistry

      4,0091929




      4,0091929










      asked Nov 14 '18 at 12:12









      Nabin SamantaNabin Samanta

      104




      104




      marked as duplicate by Neil Lunn node.js
      Users with the  node.js badge can single-handedly close node.js 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 14 '18 at 21:13


      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 node.js
      Users with the  node.js badge can single-handedly close node.js 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 14 '18 at 21:13


      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.
























          3 Answers
          3






          active

          oldest

          votes


















          0














          Try this way



           serial_number: $regex: `$search_key.*`, $options: 'i' 


          Use this same thing for maker_name also.



          This is works for me.






          share|improve this answer























          • yes its working...thanks

            – Nabin Samanta
            Nov 14 '18 at 13:06











          • Welcome @NabinSamanta. So please accept answer.

            – Sachin Shah
            Nov 14 '18 at 13:14











          • i allready voted but its says that " Thanks for feedback. Vote cast by those with less than 15 reputation are recorded"

            – Nabin Samanta
            Nov 14 '18 at 13:25











          • @NabinSamanta I'm telling accept the answer. Click on right icon. Then it will convert into green icon. :)

            – Sachin Shah
            Nov 14 '18 at 13:28











          • ok i understood

            – Nabin Samanta
            Nov 14 '18 at 13:31


















          0














          We need to pass a string to the function. so we can simply change that by passing query string.



           if(search_key!='')
          let query_string = "/^.*" + search_key + ".*$/i" // we are creating the query string here
          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 , $or: [ maker_name : query_string , serial_number : query_string ] ]
          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer























          • not working this code

            – Nabin Samanta
            Nov 14 '18 at 12:31


















          0














          try like this :



          if(search_key!='')

          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 ,
          $or: [
          maker_name : '/^.*'+ search_key +'*$/i' ,
          serial_number : '/^.*'+ search_key +'*$/i'
          ]

          ]

          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer























          • this is also not working

            – Nabin Samanta
            Nov 14 '18 at 12:31











          • Are you getting some error ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • show , what output are u getting ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • getting no error but no result

            – Nabin Samanta
            Nov 14 '18 at 12:53

















          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try this way



           serial_number: $regex: `$search_key.*`, $options: 'i' 


          Use this same thing for maker_name also.



          This is works for me.






          share|improve this answer























          • yes its working...thanks

            – Nabin Samanta
            Nov 14 '18 at 13:06











          • Welcome @NabinSamanta. So please accept answer.

            – Sachin Shah
            Nov 14 '18 at 13:14











          • i allready voted but its says that " Thanks for feedback. Vote cast by those with less than 15 reputation are recorded"

            – Nabin Samanta
            Nov 14 '18 at 13:25











          • @NabinSamanta I'm telling accept the answer. Click on right icon. Then it will convert into green icon. :)

            – Sachin Shah
            Nov 14 '18 at 13:28











          • ok i understood

            – Nabin Samanta
            Nov 14 '18 at 13:31















          0














          Try this way



           serial_number: $regex: `$search_key.*`, $options: 'i' 


          Use this same thing for maker_name also.



          This is works for me.






          share|improve this answer























          • yes its working...thanks

            – Nabin Samanta
            Nov 14 '18 at 13:06











          • Welcome @NabinSamanta. So please accept answer.

            – Sachin Shah
            Nov 14 '18 at 13:14











          • i allready voted but its says that " Thanks for feedback. Vote cast by those with less than 15 reputation are recorded"

            – Nabin Samanta
            Nov 14 '18 at 13:25











          • @NabinSamanta I'm telling accept the answer. Click on right icon. Then it will convert into green icon. :)

            – Sachin Shah
            Nov 14 '18 at 13:28











          • ok i understood

            – Nabin Samanta
            Nov 14 '18 at 13:31













          0












          0








          0







          Try this way



           serial_number: $regex: `$search_key.*`, $options: 'i' 


          Use this same thing for maker_name also.



          This is works for me.






          share|improve this answer













          Try this way



           serial_number: $regex: `$search_key.*`, $options: 'i' 


          Use this same thing for maker_name also.



          This is works for me.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 13:02









          Sachin ShahSachin Shah

          1,7301516




          1,7301516












          • yes its working...thanks

            – Nabin Samanta
            Nov 14 '18 at 13:06











          • Welcome @NabinSamanta. So please accept answer.

            – Sachin Shah
            Nov 14 '18 at 13:14











          • i allready voted but its says that " Thanks for feedback. Vote cast by those with less than 15 reputation are recorded"

            – Nabin Samanta
            Nov 14 '18 at 13:25











          • @NabinSamanta I'm telling accept the answer. Click on right icon. Then it will convert into green icon. :)

            – Sachin Shah
            Nov 14 '18 at 13:28











          • ok i understood

            – Nabin Samanta
            Nov 14 '18 at 13:31

















          • yes its working...thanks

            – Nabin Samanta
            Nov 14 '18 at 13:06











          • Welcome @NabinSamanta. So please accept answer.

            – Sachin Shah
            Nov 14 '18 at 13:14











          • i allready voted but its says that " Thanks for feedback. Vote cast by those with less than 15 reputation are recorded"

            – Nabin Samanta
            Nov 14 '18 at 13:25











          • @NabinSamanta I'm telling accept the answer. Click on right icon. Then it will convert into green icon. :)

            – Sachin Shah
            Nov 14 '18 at 13:28











          • ok i understood

            – Nabin Samanta
            Nov 14 '18 at 13:31
















          yes its working...thanks

          – Nabin Samanta
          Nov 14 '18 at 13:06





          yes its working...thanks

          – Nabin Samanta
          Nov 14 '18 at 13:06













          Welcome @NabinSamanta. So please accept answer.

          – Sachin Shah
          Nov 14 '18 at 13:14





          Welcome @NabinSamanta. So please accept answer.

          – Sachin Shah
          Nov 14 '18 at 13:14













          i allready voted but its says that " Thanks for feedback. Vote cast by those with less than 15 reputation are recorded"

          – Nabin Samanta
          Nov 14 '18 at 13:25





          i allready voted but its says that " Thanks for feedback. Vote cast by those with less than 15 reputation are recorded"

          – Nabin Samanta
          Nov 14 '18 at 13:25













          @NabinSamanta I'm telling accept the answer. Click on right icon. Then it will convert into green icon. :)

          – Sachin Shah
          Nov 14 '18 at 13:28





          @NabinSamanta I'm telling accept the answer. Click on right icon. Then it will convert into green icon. :)

          – Sachin Shah
          Nov 14 '18 at 13:28













          ok i understood

          – Nabin Samanta
          Nov 14 '18 at 13:31





          ok i understood

          – Nabin Samanta
          Nov 14 '18 at 13:31













          0














          We need to pass a string to the function. so we can simply change that by passing query string.



           if(search_key!='')
          let query_string = "/^.*" + search_key + ".*$/i" // we are creating the query string here
          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 , $or: [ maker_name : query_string , serial_number : query_string ] ]
          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer























          • not working this code

            – Nabin Samanta
            Nov 14 '18 at 12:31















          0














          We need to pass a string to the function. so we can simply change that by passing query string.



           if(search_key!='')
          let query_string = "/^.*" + search_key + ".*$/i" // we are creating the query string here
          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 , $or: [ maker_name : query_string , serial_number : query_string ] ]
          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer























          • not working this code

            – Nabin Samanta
            Nov 14 '18 at 12:31













          0












          0








          0







          We need to pass a string to the function. so we can simply change that by passing query string.



           if(search_key!='')
          let query_string = "/^.*" + search_key + ".*$/i" // we are creating the query string here
          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 , $or: [ maker_name : query_string , serial_number : query_string ] ]
          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer













          We need to pass a string to the function. so we can simply change that by passing query string.



           if(search_key!='')
          let query_string = "/^.*" + search_key + ".*$/i" // we are creating the query string here
          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 , $or: [ maker_name : query_string , serial_number : query_string ] ]
          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 12:22









          BhuvanachanduBhuvanachandu

          957




          957












          • not working this code

            – Nabin Samanta
            Nov 14 '18 at 12:31

















          • not working this code

            – Nabin Samanta
            Nov 14 '18 at 12:31
















          not working this code

          – Nabin Samanta
          Nov 14 '18 at 12:31





          not working this code

          – Nabin Samanta
          Nov 14 '18 at 12:31











          0














          try like this :



          if(search_key!='')

          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 ,
          $or: [
          maker_name : '/^.*'+ search_key +'*$/i' ,
          serial_number : '/^.*'+ search_key +'*$/i'
          ]

          ]

          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer























          • this is also not working

            – Nabin Samanta
            Nov 14 '18 at 12:31











          • Are you getting some error ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • show , what output are u getting ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • getting no error but no result

            – Nabin Samanta
            Nov 14 '18 at 12:53















          0














          try like this :



          if(search_key!='')

          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 ,
          $or: [
          maker_name : '/^.*'+ search_key +'*$/i' ,
          serial_number : '/^.*'+ search_key +'*$/i'
          ]

          ]

          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer























          • this is also not working

            – Nabin Samanta
            Nov 14 '18 at 12:31











          • Are you getting some error ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • show , what output are u getting ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • getting no error but no result

            – Nabin Samanta
            Nov 14 '18 at 12:53













          0












          0








          0







          try like this :



          if(search_key!='')

          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 ,
          $or: [
          maker_name : '/^.*'+ search_key +'*$/i' ,
          serial_number : '/^.*'+ search_key +'*$/i'
          ]

          ]

          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );







          share|improve this answer













          try like this :



          if(search_key!='')

          dbo.collection("assets").aggregate([

          "$match": $and: [ status: 1 ,
          $or: [
          maker_name : '/^.*'+ search_key +'*$/i' ,
          serial_number : '/^.*'+ search_key +'*$/i'
          ]

          ]

          ,

          $lookup:

          from: 'asset_type',
          localField: 'asset_type',
          foreignField: 'asset_type_id',
          as: 'asset_type_details'


          ]).sort(_id:-1).toArray(function(err, result)
          if (err) throw err;
          res.status(200).json('return_data': result );
          db.close();
          );








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 12:22









          Saurabh MistrySaurabh Mistry

          4,0091929




          4,0091929












          • this is also not working

            – Nabin Samanta
            Nov 14 '18 at 12:31











          • Are you getting some error ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • show , what output are u getting ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • getting no error but no result

            – Nabin Samanta
            Nov 14 '18 at 12:53

















          • this is also not working

            – Nabin Samanta
            Nov 14 '18 at 12:31











          • Are you getting some error ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • show , what output are u getting ?

            – Saurabh Mistry
            Nov 14 '18 at 12:38











          • getting no error but no result

            – Nabin Samanta
            Nov 14 '18 at 12:53
















          this is also not working

          – Nabin Samanta
          Nov 14 '18 at 12:31





          this is also not working

          – Nabin Samanta
          Nov 14 '18 at 12:31













          Are you getting some error ?

          – Saurabh Mistry
          Nov 14 '18 at 12:38





          Are you getting some error ?

          – Saurabh Mistry
          Nov 14 '18 at 12:38













          show , what output are u getting ?

          – Saurabh Mistry
          Nov 14 '18 at 12:38





          show , what output are u getting ?

          – Saurabh Mistry
          Nov 14 '18 at 12:38













          getting no error but no result

          – Nabin Samanta
          Nov 14 '18 at 12:53





          getting no error but no result

          – Nabin Samanta
          Nov 14 '18 at 12:53



          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus