MongoDB regex string startswith and endswith [duplicate]












0
















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.










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 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
















0
















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.










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 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














0












0








0









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.










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 16:27









user56130user56130

111




111




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 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 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 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



















  • 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












4 Answers
4






active

oldest

votes


















1














combine both querys with a AND opetator



db.usuarios.find({
$and:[
{nome:{$regex: /^ma/ }},
{nome:{$regex: /l$/ }}
]

})





share|improve this answer
























  • The answer is right, but $regex is not necessary here. {nome: /^ma/} is fine.

    – yaoxing
    Nov 25 '18 at 18:04



















1














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.






share|improve this answer































    0














    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.






    share|improve this answer































      0














      May be this code help you.



      you can search small letter string and capital letter string.......



      enter image description here






      share|improve this answer
































        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        combine both querys with a AND opetator



        db.usuarios.find({
        $and:[
        {nome:{$regex: /^ma/ }},
        {nome:{$regex: /l$/ }}
        ]

        })





        share|improve this answer
























        • The answer is right, but $regex is not necessary here. {nome: /^ma/} is fine.

          – yaoxing
          Nov 25 '18 at 18:04
















        1














        combine both querys with a AND opetator



        db.usuarios.find({
        $and:[
        {nome:{$regex: /^ma/ }},
        {nome:{$regex: /l$/ }}
        ]

        })





        share|improve this answer
























        • The answer is right, but $regex is not necessary here. {nome: /^ma/} is fine.

          – yaoxing
          Nov 25 '18 at 18:04














        1












        1








        1







        combine both querys with a AND opetator



        db.usuarios.find({
        $and:[
        {nome:{$regex: /^ma/ }},
        {nome:{$regex: /l$/ }}
        ]

        })





        share|improve this answer













        combine both querys with a AND opetator



        db.usuarios.find({
        $and:[
        {nome:{$regex: /^ma/ }},
        {nome:{$regex: /l$/ }}
        ]

        })






        share|improve this answer












        share|improve this answer



        share|improve this answer










        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



















        • 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













        1














        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.






        share|improve this answer




























          1














          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.






          share|improve this answer


























            1












            1








            1







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 19:01









            JohnnyHKJohnnyHK

            212k41451375




            212k41451375























                0














                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.






                share|improve this answer




























                  0














                  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.






                  share|improve this answer


























                    0












                    0








                    0







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 18:12









                    yaoxingyaoxing

                    2,46821326




                    2,46821326























                        0














                        May be this code help you.



                        you can search small letter string and capital letter string.......



                        enter image description here






                        share|improve this answer






























                          0














                          May be this code help you.



                          you can search small letter string and capital letter string.......



                          enter image description here






                          share|improve this answer




























                            0












                            0








                            0







                            May be this code help you.



                            you can search small letter string and capital letter string.......



                            enter image description here






                            share|improve this answer















                            May be this code help you.



                            you can search small letter string and capital letter string.......



                            enter image description here







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 25 '18 at 18:19

























                            answered Nov 25 '18 at 18:14









                            Atul KumarAtul Kumar

                            215




                            215















                                Popular posts from this blog

                                Wiesbaden

                                Marschland

                                Dieringhausen