MongoDB regex string startswith and endswith [duplicate]
This question already has an answer here:
Regex matching beginning AND end strings
4 answers
Regular Expression to find string starts with letter and ends with slash /
3 answers
So i have something that looks like this
db.usuarios.insert
(
[
{
"nome" : "neymala",
"idade" : 40,
"status" : "solteira"
},
{
"nome" : "gabriel",
"idade" : 31,
"status" : "casado"
},
{
"nome" : "jose",
"idade" : 25,
"status" : "solteiro"
},
{
"nome" : "manoel",
"idade" : 25,
"status" : "solteiro",
"interesses" : [
"esporte",
"musica"
]
}
]
)
I would like to find names that starts with ma and ends with l, for example "manoel" or "manuel"
I have figured out how to do one or the other with the fallowing querys:
db.usuarios.find({nome:{$regex: /^ma/ }})
db.usuarios.find({nome:{$regex: /l$/ }})
Now i would like to combine them into a single query.
json regex 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 25 '18 at 19:34
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:
Regex matching beginning AND end strings
4 answers
Regular Expression to find string starts with letter and ends with slash /
3 answers
So i have something that looks like this
db.usuarios.insert
(
[
{
"nome" : "neymala",
"idade" : 40,
"status" : "solteira"
},
{
"nome" : "gabriel",
"idade" : 31,
"status" : "casado"
},
{
"nome" : "jose",
"idade" : 25,
"status" : "solteiro"
},
{
"nome" : "manoel",
"idade" : 25,
"status" : "solteiro",
"interesses" : [
"esporte",
"musica"
]
}
]
)
I would like to find names that starts with ma and ends with l, for example "manoel" or "manuel"
I have figured out how to do one or the other with the fallowing querys:
db.usuarios.find({nome:{$regex: /^ma/ }})
db.usuarios.find({nome:{$regex: /l$/ }})
Now i would like to combine them into a single query.
json regex 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 25 '18 at 19:34
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.
Also refer to want mongo query to do string regex start && contains && end since this is one of the worst things you can possibly do. All query forms will not be able to use an index
– Neil Lunn
Nov 25 '18 at 19:36
add a comment |
This question already has an answer here:
Regex matching beginning AND end strings
4 answers
Regular Expression to find string starts with letter and ends with slash /
3 answers
So i have something that looks like this
db.usuarios.insert
(
[
{
"nome" : "neymala",
"idade" : 40,
"status" : "solteira"
},
{
"nome" : "gabriel",
"idade" : 31,
"status" : "casado"
},
{
"nome" : "jose",
"idade" : 25,
"status" : "solteiro"
},
{
"nome" : "manoel",
"idade" : 25,
"status" : "solteiro",
"interesses" : [
"esporte",
"musica"
]
}
]
)
I would like to find names that starts with ma and ends with l, for example "manoel" or "manuel"
I have figured out how to do one or the other with the fallowing querys:
db.usuarios.find({nome:{$regex: /^ma/ }})
db.usuarios.find({nome:{$regex: /l$/ }})
Now i would like to combine them into a single query.
json regex mongodb
This question already has an answer here:
Regex matching beginning AND end strings
4 answers
Regular Expression to find string starts with letter and ends with slash /
3 answers
So i have something that looks like this
db.usuarios.insert
(
[
{
"nome" : "neymala",
"idade" : 40,
"status" : "solteira"
},
{
"nome" : "gabriel",
"idade" : 31,
"status" : "casado"
},
{
"nome" : "jose",
"idade" : 25,
"status" : "solteiro"
},
{
"nome" : "manoel",
"idade" : 25,
"status" : "solteiro",
"interesses" : [
"esporte",
"musica"
]
}
]
)
I would like to find names that starts with ma and ends with l, for example "manoel" or "manuel"
I have figured out how to do one or the other with the fallowing querys:
db.usuarios.find({nome:{$regex: /^ma/ }})
db.usuarios.find({nome:{$regex: /l$/ }})
Now i would like to combine them into a single query.
This question already has an answer here:
Regex matching beginning AND end strings
4 answers
Regular Expression to find string starts with letter and ends with slash /
3 answers
json regex mongodb
json regex mongodb
asked Nov 25 '18 at 16:27
user56130user56130
111
111
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 25 '18 at 19:34
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 25 '18 at 19:34
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.
Also refer to want mongo query to do string regex start && contains && end since this is one of the worst things you can possibly do. All query forms will not be able to use an index
– Neil Lunn
Nov 25 '18 at 19:36
add a comment |
Also refer to want mongo query to do string regex start && contains && end since this is one of the worst things you can possibly do. All query forms will not be able to use an index
– Neil Lunn
Nov 25 '18 at 19:36
Also refer to want mongo query to do string regex start && contains && end since this is one of the worst things you can possibly do. All query forms will not be able to use an index
– Neil Lunn
Nov 25 '18 at 19:36
Also refer to want mongo query to do string regex start && contains && end since this is one of the worst things you can possibly do. All query forms will not be able to use an index
– Neil Lunn
Nov 25 '18 at 19:36
add a comment |
4 Answers
4
active
oldest
votes
combine both querys with a AND opetator
db.usuarios.find({
$and:[
{nome:{$regex: /^ma/ }},
{nome:{$regex: /l$/ }}
]
})
The answer is right, but$regex
is not necessary here.{nome: /^ma/}
is fine.
– yaoxing
Nov 25 '18 at 18:04
add a comment |
You can combine the two requirements into a single regex:
db.usuarios.find({nome: /^ma.*l$/})
In a regex, .*
means to match 0 or more of any character. So this regex matches names that start with ma
and end with l
, ignoring whatever is between.
add a comment |
In javascript /.../
is a regex, so no $regex
needed.
db.usuarios.find({
$and:[
{nome: /^ma/ },
{nome: /l$/ }
]
})
Also note starts with
can hit an index. ends with
can't. This way you'd better have a selective starts with
, otherwise there will be lots of object scans which may occupy extra CPU and possibly slow down your query.
add a comment |
May be this code help you.
you can search small letter string and capital letter string.......
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
combine both querys with a AND opetator
db.usuarios.find({
$and:[
{nome:{$regex: /^ma/ }},
{nome:{$regex: /l$/ }}
]
})
The answer is right, but$regex
is not necessary here.{nome: /^ma/}
is fine.
– yaoxing
Nov 25 '18 at 18:04
add a comment |
combine both querys with a AND opetator
db.usuarios.find({
$and:[
{nome:{$regex: /^ma/ }},
{nome:{$regex: /l$/ }}
]
})
The answer is right, but$regex
is not necessary here.{nome: /^ma/}
is fine.
– yaoxing
Nov 25 '18 at 18:04
add a comment |
combine both querys with a AND opetator
db.usuarios.find({
$and:[
{nome:{$regex: /^ma/ }},
{nome:{$regex: /l$/ }}
]
})
combine both querys with a AND opetator
db.usuarios.find({
$and:[
{nome:{$regex: /^ma/ }},
{nome:{$regex: /l$/ }}
]
})
answered Nov 25 '18 at 17:05
user56130user56130
111
111
The answer is right, but$regex
is not necessary here.{nome: /^ma/}
is fine.
– yaoxing
Nov 25 '18 at 18:04
add a comment |
The answer is right, but$regex
is not necessary here.{nome: /^ma/}
is fine.
– yaoxing
Nov 25 '18 at 18:04
The answer is right, but
$regex
is not necessary here. {nome: /^ma/}
is fine.– yaoxing
Nov 25 '18 at 18:04
The answer is right, but
$regex
is not necessary here. {nome: /^ma/}
is fine.– yaoxing
Nov 25 '18 at 18:04
add a comment |
You can combine the two requirements into a single regex:
db.usuarios.find({nome: /^ma.*l$/})
In a regex, .*
means to match 0 or more of any character. So this regex matches names that start with ma
and end with l
, ignoring whatever is between.
add a comment |
You can combine the two requirements into a single regex:
db.usuarios.find({nome: /^ma.*l$/})
In a regex, .*
means to match 0 or more of any character. So this regex matches names that start with ma
and end with l
, ignoring whatever is between.
add a comment |
You can combine the two requirements into a single regex:
db.usuarios.find({nome: /^ma.*l$/})
In a regex, .*
means to match 0 or more of any character. So this regex matches names that start with ma
and end with l
, ignoring whatever is between.
You can combine the two requirements into a single regex:
db.usuarios.find({nome: /^ma.*l$/})
In a regex, .*
means to match 0 or more of any character. So this regex matches names that start with ma
and end with l
, ignoring whatever is between.
answered Nov 25 '18 at 19:01
JohnnyHKJohnnyHK
212k41451375
212k41451375
add a comment |
add a comment |
In javascript /.../
is a regex, so no $regex
needed.
db.usuarios.find({
$and:[
{nome: /^ma/ },
{nome: /l$/ }
]
})
Also note starts with
can hit an index. ends with
can't. This way you'd better have a selective starts with
, otherwise there will be lots of object scans which may occupy extra CPU and possibly slow down your query.
add a comment |
In javascript /.../
is a regex, so no $regex
needed.
db.usuarios.find({
$and:[
{nome: /^ma/ },
{nome: /l$/ }
]
})
Also note starts with
can hit an index. ends with
can't. This way you'd better have a selective starts with
, otherwise there will be lots of object scans which may occupy extra CPU and possibly slow down your query.
add a comment |
In javascript /.../
is a regex, so no $regex
needed.
db.usuarios.find({
$and:[
{nome: /^ma/ },
{nome: /l$/ }
]
})
Also note starts with
can hit an index. ends with
can't. This way you'd better have a selective starts with
, otherwise there will be lots of object scans which may occupy extra CPU and possibly slow down your query.
In javascript /.../
is a regex, so no $regex
needed.
db.usuarios.find({
$and:[
{nome: /^ma/ },
{nome: /l$/ }
]
})
Also note starts with
can hit an index. ends with
can't. This way you'd better have a selective starts with
, otherwise there will be lots of object scans which may occupy extra CPU and possibly slow down your query.
answered Nov 25 '18 at 18:12
yaoxingyaoxing
2,46821326
2,46821326
add a comment |
add a comment |
May be this code help you.
you can search small letter string and capital letter string.......
add a comment |
May be this code help you.
you can search small letter string and capital letter string.......
add a comment |
May be this code help you.
you can search small letter string and capital letter string.......
May be this code help you.
you can search small letter string and capital letter string.......
edited Nov 25 '18 at 18:19
answered Nov 25 '18 at 18:14
Atul KumarAtul Kumar
215
215
add a comment |
add a comment |
Also refer to want mongo query to do string regex start && contains && end since this is one of the worst things you can possibly do. All query forms will not be able to use an index
– Neil Lunn
Nov 25 '18 at 19:36