Occurance of two words with specific initials MongoDB












0















I have a collection of movies structured with title, writers, rated, actors and others fields.



I am trying to find movies with at least one actors with name and lastname starting with a C and show the title and the actors.
Actors is an array with actors (first name then last name on the same field) for example: {Matt Damon, Bradd Pitt} etc...



I tried with this :



db.movieDetails.find( 
{ actors: { $regex: "^C.C" , $options: 'i' } },
{_id:0, title:1, actors:1}
);


and also with aggregation like this :



db.movieDetails.aggregate(
{$unwind: '$actors'},
{$match: { actors: { $regex: "^C.C", $options: 'i' } } },
{$group: {_id: { actors: '$actors' , title: "$title" }}}
);


But I'm just trying to avoid the problem by looking when there is an actor starting with C and another occurrence, and this solution works only partially because it returns me movies with two actors with only their first name starting with a C.



I would like to know if there is a way to use regex for example to check all words starting with something in the whole string.










share|improve this question





























    0















    I have a collection of movies structured with title, writers, rated, actors and others fields.



    I am trying to find movies with at least one actors with name and lastname starting with a C and show the title and the actors.
    Actors is an array with actors (first name then last name on the same field) for example: {Matt Damon, Bradd Pitt} etc...



    I tried with this :



    db.movieDetails.find( 
    { actors: { $regex: "^C.C" , $options: 'i' } },
    {_id:0, title:1, actors:1}
    );


    and also with aggregation like this :



    db.movieDetails.aggregate(
    {$unwind: '$actors'},
    {$match: { actors: { $regex: "^C.C", $options: 'i' } } },
    {$group: {_id: { actors: '$actors' , title: "$title" }}}
    );


    But I'm just trying to avoid the problem by looking when there is an actor starting with C and another occurrence, and this solution works only partially because it returns me movies with two actors with only their first name starting with a C.



    I would like to know if there is a way to use regex for example to check all words starting with something in the whole string.










    share|improve this question



























      0












      0








      0








      I have a collection of movies structured with title, writers, rated, actors and others fields.



      I am trying to find movies with at least one actors with name and lastname starting with a C and show the title and the actors.
      Actors is an array with actors (first name then last name on the same field) for example: {Matt Damon, Bradd Pitt} etc...



      I tried with this :



      db.movieDetails.find( 
      { actors: { $regex: "^C.C" , $options: 'i' } },
      {_id:0, title:1, actors:1}
      );


      and also with aggregation like this :



      db.movieDetails.aggregate(
      {$unwind: '$actors'},
      {$match: { actors: { $regex: "^C.C", $options: 'i' } } },
      {$group: {_id: { actors: '$actors' , title: "$title" }}}
      );


      But I'm just trying to avoid the problem by looking when there is an actor starting with C and another occurrence, and this solution works only partially because it returns me movies with two actors with only their first name starting with a C.



      I would like to know if there is a way to use regex for example to check all words starting with something in the whole string.










      share|improve this question
















      I have a collection of movies structured with title, writers, rated, actors and others fields.



      I am trying to find movies with at least one actors with name and lastname starting with a C and show the title and the actors.
      Actors is an array with actors (first name then last name on the same field) for example: {Matt Damon, Bradd Pitt} etc...



      I tried with this :



      db.movieDetails.find( 
      { actors: { $regex: "^C.C" , $options: 'i' } },
      {_id:0, title:1, actors:1}
      );


      and also with aggregation like this :



      db.movieDetails.aggregate(
      {$unwind: '$actors'},
      {$match: { actors: { $regex: "^C.C", $options: 'i' } } },
      {$group: {_id: { actors: '$actors' , title: "$title" }}}
      );


      But I'm just trying to avoid the problem by looking when there is an actor starting with C and another occurrence, and this solution works only partially because it returns me movies with two actors with only their first name starting with a C.



      I would like to know if there is a way to use regex for example to check all words starting with something in the whole string.







      javascript regex string mongodb nosql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 14:35









      asemahle

      8,92132731




      8,92132731










      asked Nov 27 '18 at 14:24









      Raphael LopesRaphael Lopes

      204




      204
























          1 Answer
          1






          active

          oldest

          votes


















          2














          The regex that you have provided is incorrect. An explanation of ^C.C





          • ^ - Matches the start of a line


          • C - Matches the character C


          • . - Matches any single character


          • C - Matches the character C


          Basically, it will match any actor name where the 1st and 3rd letter are C.



          Instead, try this regex: ^C.* C. Explanation:





          • ^ - Matches the start of a line


          • C - Matches the character C


          • .* - Matches any character between zero and unlimited times


          • (space)C - Matches a space followed by a C (this assumes the last name is separated from the first name by a space)


          Mongo code:



          db.movieDetails.find({
          actors: {
          $regex: "^C.* C",
          $options: "i"
          }
          });





          share|improve this answer
























          • Thanks, your solution worked perfectly and now i fully understand regex.

            – Raphael Lopes
            Nov 28 '18 at 7:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501846%2foccurance-of-two-words-with-specific-initials-mongodb%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          The regex that you have provided is incorrect. An explanation of ^C.C





          • ^ - Matches the start of a line


          • C - Matches the character C


          • . - Matches any single character


          • C - Matches the character C


          Basically, it will match any actor name where the 1st and 3rd letter are C.



          Instead, try this regex: ^C.* C. Explanation:





          • ^ - Matches the start of a line


          • C - Matches the character C


          • .* - Matches any character between zero and unlimited times


          • (space)C - Matches a space followed by a C (this assumes the last name is separated from the first name by a space)


          Mongo code:



          db.movieDetails.find({
          actors: {
          $regex: "^C.* C",
          $options: "i"
          }
          });





          share|improve this answer
























          • Thanks, your solution worked perfectly and now i fully understand regex.

            – Raphael Lopes
            Nov 28 '18 at 7:40
















          2














          The regex that you have provided is incorrect. An explanation of ^C.C





          • ^ - Matches the start of a line


          • C - Matches the character C


          • . - Matches any single character


          • C - Matches the character C


          Basically, it will match any actor name where the 1st and 3rd letter are C.



          Instead, try this regex: ^C.* C. Explanation:





          • ^ - Matches the start of a line


          • C - Matches the character C


          • .* - Matches any character between zero and unlimited times


          • (space)C - Matches a space followed by a C (this assumes the last name is separated from the first name by a space)


          Mongo code:



          db.movieDetails.find({
          actors: {
          $regex: "^C.* C",
          $options: "i"
          }
          });





          share|improve this answer
























          • Thanks, your solution worked perfectly and now i fully understand regex.

            – Raphael Lopes
            Nov 28 '18 at 7:40














          2












          2








          2







          The regex that you have provided is incorrect. An explanation of ^C.C





          • ^ - Matches the start of a line


          • C - Matches the character C


          • . - Matches any single character


          • C - Matches the character C


          Basically, it will match any actor name where the 1st and 3rd letter are C.



          Instead, try this regex: ^C.* C. Explanation:





          • ^ - Matches the start of a line


          • C - Matches the character C


          • .* - Matches any character between zero and unlimited times


          • (space)C - Matches a space followed by a C (this assumes the last name is separated from the first name by a space)


          Mongo code:



          db.movieDetails.find({
          actors: {
          $regex: "^C.* C",
          $options: "i"
          }
          });





          share|improve this answer













          The regex that you have provided is incorrect. An explanation of ^C.C





          • ^ - Matches the start of a line


          • C - Matches the character C


          • . - Matches any single character


          • C - Matches the character C


          Basically, it will match any actor name where the 1st and 3rd letter are C.



          Instead, try this regex: ^C.* C. Explanation:





          • ^ - Matches the start of a line


          • C - Matches the character C


          • .* - Matches any character between zero and unlimited times


          • (space)C - Matches a space followed by a C (this assumes the last name is separated from the first name by a space)


          Mongo code:



          db.movieDetails.find({
          actors: {
          $regex: "^C.* C",
          $options: "i"
          }
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 15:05









          asemahleasemahle

          8,92132731




          8,92132731













          • Thanks, your solution worked perfectly and now i fully understand regex.

            – Raphael Lopes
            Nov 28 '18 at 7:40



















          • Thanks, your solution worked perfectly and now i fully understand regex.

            – Raphael Lopes
            Nov 28 '18 at 7:40

















          Thanks, your solution worked perfectly and now i fully understand regex.

          – Raphael Lopes
          Nov 28 '18 at 7:40





          Thanks, your solution worked perfectly and now i fully understand regex.

          – Raphael Lopes
          Nov 28 '18 at 7:40




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501846%2foccurance-of-two-words-with-specific-initials-mongodb%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)