how to use variable as a field value in mongodb query? [duplicate]
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();
);
node.js mongodb
marked as duplicate by Neil Lunn
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.
add a comment |
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();
);
node.js mongodb
marked as duplicate by Neil Lunn
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.
add a comment |
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();
);
node.js mongodb
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
node.js mongodb
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
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
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.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try this way
serial_number: $regex: `$search_key.*`, $options: 'i'
Use this same thing for maker_name also.
This is works for me.
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
|
show 1 more comment
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();
);
not working this code
– Nabin Samanta
Nov 14 '18 at 12:31
add a comment |
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();
);
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
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this way
serial_number: $regex: `$search_key.*`, $options: 'i'
Use this same thing for maker_name also.
This is works for me.
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
|
show 1 more comment
Try this way
serial_number: $regex: `$search_key.*`, $options: 'i'
Use this same thing for maker_name also.
This is works for me.
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
|
show 1 more comment
Try this way
serial_number: $regex: `$search_key.*`, $options: 'i'
Use this same thing for maker_name also.
This is works for me.
Try this way
serial_number: $regex: `$search_key.*`, $options: 'i'
Use this same thing for maker_name also.
This is works for me.
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
|
show 1 more comment
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
|
show 1 more comment
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();
);
not working this code
– Nabin Samanta
Nov 14 '18 at 12:31
add a comment |
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();
);
not working this code
– Nabin Samanta
Nov 14 '18 at 12:31
add a comment |
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();
);
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();
);
answered Nov 14 '18 at 12:22
BhuvanachanduBhuvanachandu
957
957
not working this code
– Nabin Samanta
Nov 14 '18 at 12:31
add a comment |
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
add a comment |
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();
);
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
add a comment |
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();
);
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
add a comment |
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();
);
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();
);
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
add a comment |
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
add a comment |