Numeric comparisons such as greater-than on a currency amount stored as a string





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















So I have an example JSON file with fake bank users, and I want to find the people with a balance greater than $1000, how can I do so? Do keep in mind the balance is a String value.
EXAMPLE USER INFO:



{
"_id" : ObjectId("58d1cae3cba624106c8080ab"),
"isActive" : false,
"balance" : "$3,495.58",
"age" : 24,
"eyeColor" : "blue",
"name" : "Webster Sanders",
"gender" : "male",
"company" : "HALAP",
"email" : "webstersanders@halap.com",
"phone" : "+1 (883) 536-2259",
"address" : "300 Jewel Street, Sugartown, Federated States Of Micronesia, 9305"
}









share|improve this question

























  • Are you going to use the command inside an application or just a single run in the mongo shell?

    – Thomas Bormans
    Mar 22 '17 at 7:18











  • @ThomasBormans in a mongo shell

    – no name
    Mar 22 '17 at 21:14


















0















So I have an example JSON file with fake bank users, and I want to find the people with a balance greater than $1000, how can I do so? Do keep in mind the balance is a String value.
EXAMPLE USER INFO:



{
"_id" : ObjectId("58d1cae3cba624106c8080ab"),
"isActive" : false,
"balance" : "$3,495.58",
"age" : 24,
"eyeColor" : "blue",
"name" : "Webster Sanders",
"gender" : "male",
"company" : "HALAP",
"email" : "webstersanders@halap.com",
"phone" : "+1 (883) 536-2259",
"address" : "300 Jewel Street, Sugartown, Federated States Of Micronesia, 9305"
}









share|improve this question

























  • Are you going to use the command inside an application or just a single run in the mongo shell?

    – Thomas Bormans
    Mar 22 '17 at 7:18











  • @ThomasBormans in a mongo shell

    – no name
    Mar 22 '17 at 21:14














0












0








0


2






So I have an example JSON file with fake bank users, and I want to find the people with a balance greater than $1000, how can I do so? Do keep in mind the balance is a String value.
EXAMPLE USER INFO:



{
"_id" : ObjectId("58d1cae3cba624106c8080ab"),
"isActive" : false,
"balance" : "$3,495.58",
"age" : 24,
"eyeColor" : "blue",
"name" : "Webster Sanders",
"gender" : "male",
"company" : "HALAP",
"email" : "webstersanders@halap.com",
"phone" : "+1 (883) 536-2259",
"address" : "300 Jewel Street, Sugartown, Federated States Of Micronesia, 9305"
}









share|improve this question
















So I have an example JSON file with fake bank users, and I want to find the people with a balance greater than $1000, how can I do so? Do keep in mind the balance is a String value.
EXAMPLE USER INFO:



{
"_id" : ObjectId("58d1cae3cba624106c8080ab"),
"isActive" : false,
"balance" : "$3,495.58",
"age" : 24,
"eyeColor" : "blue",
"name" : "Webster Sanders",
"gender" : "male",
"company" : "HALAP",
"email" : "webstersanders@halap.com",
"phone" : "+1 (883) 536-2259",
"address" : "300 Jewel Street, Sugartown, Federated States Of Micronesia, 9305"
}






mongodb mongodb-query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 '17 at 15:36









Vince Bowdren

4,43721841




4,43721841










asked Mar 22 '17 at 4:07









no nameno name

726




726













  • Are you going to use the command inside an application or just a single run in the mongo shell?

    – Thomas Bormans
    Mar 22 '17 at 7:18











  • @ThomasBormans in a mongo shell

    – no name
    Mar 22 '17 at 21:14



















  • Are you going to use the command inside an application or just a single run in the mongo shell?

    – Thomas Bormans
    Mar 22 '17 at 7:18











  • @ThomasBormans in a mongo shell

    – no name
    Mar 22 '17 at 21:14

















Are you going to use the command inside an application or just a single run in the mongo shell?

– Thomas Bormans
Mar 22 '17 at 7:18





Are you going to use the command inside an application or just a single run in the mongo shell?

– Thomas Bormans
Mar 22 '17 at 7:18













@ThomasBormans in a mongo shell

– no name
Mar 22 '17 at 21:14





@ThomasBormans in a mongo shell

– no name
Mar 22 '17 at 21:14












5 Answers
5






active

oldest

votes


















1














You could use a regular expression like:



db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });





share|improve this answer


























  • im sorry but can you explain to me what the command / expression does?

    – no name
    Mar 22 '17 at 21:03











  • The regex works by making sure the first number is greater than 1 and that it is followed by 3 or more numbers. So to break it down: ^ means from the start of the line. $ matches the $ symbol. [1-9] matches the numbers from 1 to 9. [0-9] matches all numbers. {3,} match the previous pattern at least 3 times.

    – Brmmmm
    Mar 22 '17 at 21:25













  • nothing happends: gyazo.com/b5610afb389ea8c3ffc34cfe5cc35f89

    – no name
    Mar 23 '17 at 0:26











  • I have edited the regular expression to handle the ',' in the balance ([0-9,]). It should work now.

    – Brmmmm
    Mar 23 '17 at 15:33



















0














You are making life very difficult for yourself by storing the monetary value as a string. You would be better off storing it as a numeric value, possibly with a string equivalent (for presentation) in a second field if necessary.






share|improve this answer































    0














    Number(currency.replace(/[^0-9.-]+/g,""));






    share|improve this answer
























    • var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));

      – jignesh
      Jan 3 at 11:02



















    0














    please try below code



    var currency = "-$4,400.50";
    var number = Number(currency.replace(/[^0-9.-]+/g,""));






    share|improve this answer































      -1














      First get balance in string, and then replace $ and , with '' (blank quotes) then use int.parse.



      For example :



      string a = balance
      a = a.replace("$","");
      a = a.replace(",","");
      int balance = int.parse(a);





      share|improve this answer


























      • im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc

        – no name
        Mar 22 '17 at 4:41












      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%2f42942580%2fnumeric-comparisons-such-as-greater-than-on-a-currency-amount-stored-as-a-string%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You could use a regular expression like:



      db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });





      share|improve this answer


























      • im sorry but can you explain to me what the command / expression does?

        – no name
        Mar 22 '17 at 21:03











      • The regex works by making sure the first number is greater than 1 and that it is followed by 3 or more numbers. So to break it down: ^ means from the start of the line. $ matches the $ symbol. [1-9] matches the numbers from 1 to 9. [0-9] matches all numbers. {3,} match the previous pattern at least 3 times.

        – Brmmmm
        Mar 22 '17 at 21:25













      • nothing happends: gyazo.com/b5610afb389ea8c3ffc34cfe5cc35f89

        – no name
        Mar 23 '17 at 0:26











      • I have edited the regular expression to handle the ',' in the balance ([0-9,]). It should work now.

        – Brmmmm
        Mar 23 '17 at 15:33
















      1














      You could use a regular expression like:



      db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });





      share|improve this answer


























      • im sorry but can you explain to me what the command / expression does?

        – no name
        Mar 22 '17 at 21:03











      • The regex works by making sure the first number is greater than 1 and that it is followed by 3 or more numbers. So to break it down: ^ means from the start of the line. $ matches the $ symbol. [1-9] matches the numbers from 1 to 9. [0-9] matches all numbers. {3,} match the previous pattern at least 3 times.

        – Brmmmm
        Mar 22 '17 at 21:25













      • nothing happends: gyazo.com/b5610afb389ea8c3ffc34cfe5cc35f89

        – no name
        Mar 23 '17 at 0:26











      • I have edited the regular expression to handle the ',' in the balance ([0-9,]). It should work now.

        – Brmmmm
        Mar 23 '17 at 15:33














      1












      1








      1







      You could use a regular expression like:



      db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });





      share|improve this answer















      You could use a regular expression like:



      db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 23 '17 at 15:32

























      answered Mar 22 '17 at 10:06









      BrmmmmBrmmmm

      1326




      1326













      • im sorry but can you explain to me what the command / expression does?

        – no name
        Mar 22 '17 at 21:03











      • The regex works by making sure the first number is greater than 1 and that it is followed by 3 or more numbers. So to break it down: ^ means from the start of the line. $ matches the $ symbol. [1-9] matches the numbers from 1 to 9. [0-9] matches all numbers. {3,} match the previous pattern at least 3 times.

        – Brmmmm
        Mar 22 '17 at 21:25













      • nothing happends: gyazo.com/b5610afb389ea8c3ffc34cfe5cc35f89

        – no name
        Mar 23 '17 at 0:26











      • I have edited the regular expression to handle the ',' in the balance ([0-9,]). It should work now.

        – Brmmmm
        Mar 23 '17 at 15:33



















      • im sorry but can you explain to me what the command / expression does?

        – no name
        Mar 22 '17 at 21:03











      • The regex works by making sure the first number is greater than 1 and that it is followed by 3 or more numbers. So to break it down: ^ means from the start of the line. $ matches the $ symbol. [1-9] matches the numbers from 1 to 9. [0-9] matches all numbers. {3,} match the previous pattern at least 3 times.

        – Brmmmm
        Mar 22 '17 at 21:25













      • nothing happends: gyazo.com/b5610afb389ea8c3ffc34cfe5cc35f89

        – no name
        Mar 23 '17 at 0:26











      • I have edited the regular expression to handle the ',' in the balance ([0-9,]). It should work now.

        – Brmmmm
        Mar 23 '17 at 15:33

















      im sorry but can you explain to me what the command / expression does?

      – no name
      Mar 22 '17 at 21:03





      im sorry but can you explain to me what the command / expression does?

      – no name
      Mar 22 '17 at 21:03













      The regex works by making sure the first number is greater than 1 and that it is followed by 3 or more numbers. So to break it down: ^ means from the start of the line. $ matches the $ symbol. [1-9] matches the numbers from 1 to 9. [0-9] matches all numbers. {3,} match the previous pattern at least 3 times.

      – Brmmmm
      Mar 22 '17 at 21:25







      The regex works by making sure the first number is greater than 1 and that it is followed by 3 or more numbers. So to break it down: ^ means from the start of the line. $ matches the $ symbol. [1-9] matches the numbers from 1 to 9. [0-9] matches all numbers. {3,} match the previous pattern at least 3 times.

      – Brmmmm
      Mar 22 '17 at 21:25















      nothing happends: gyazo.com/b5610afb389ea8c3ffc34cfe5cc35f89

      – no name
      Mar 23 '17 at 0:26





      nothing happends: gyazo.com/b5610afb389ea8c3ffc34cfe5cc35f89

      – no name
      Mar 23 '17 at 0:26













      I have edited the regular expression to handle the ',' in the balance ([0-9,]). It should work now.

      – Brmmmm
      Mar 23 '17 at 15:33





      I have edited the regular expression to handle the ',' in the balance ([0-9,]). It should work now.

      – Brmmmm
      Mar 23 '17 at 15:33













      0














      You are making life very difficult for yourself by storing the monetary value as a string. You would be better off storing it as a numeric value, possibly with a string equivalent (for presentation) in a second field if necessary.






      share|improve this answer




























        0














        You are making life very difficult for yourself by storing the monetary value as a string. You would be better off storing it as a numeric value, possibly with a string equivalent (for presentation) in a second field if necessary.






        share|improve this answer


























          0












          0








          0







          You are making life very difficult for yourself by storing the monetary value as a string. You would be better off storing it as a numeric value, possibly with a string equivalent (for presentation) in a second field if necessary.






          share|improve this answer













          You are making life very difficult for yourself by storing the monetary value as a string. You would be better off storing it as a numeric value, possibly with a string equivalent (for presentation) in a second field if necessary.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 '17 at 15:35









          Vince BowdrenVince Bowdren

          4,43721841




          4,43721841























              0














              Number(currency.replace(/[^0-9.-]+/g,""));






              share|improve this answer
























              • var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));

                – jignesh
                Jan 3 at 11:02
















              0














              Number(currency.replace(/[^0-9.-]+/g,""));






              share|improve this answer
























              • var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));

                – jignesh
                Jan 3 at 11:02














              0












              0








              0







              Number(currency.replace(/[^0-9.-]+/g,""));






              share|improve this answer













              Number(currency.replace(/[^0-9.-]+/g,""));







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 3 at 11:02









              jigneshjignesh

              1




              1













              • var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));

                – jignesh
                Jan 3 at 11:02



















              • var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));

                – jignesh
                Jan 3 at 11:02

















              var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));

              – jignesh
              Jan 3 at 11:02





              var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));

              – jignesh
              Jan 3 at 11:02











              0














              please try below code



              var currency = "-$4,400.50";
              var number = Number(currency.replace(/[^0-9.-]+/g,""));






              share|improve this answer




























                0














                please try below code



                var currency = "-$4,400.50";
                var number = Number(currency.replace(/[^0-9.-]+/g,""));






                share|improve this answer


























                  0












                  0








                  0







                  please try below code



                  var currency = "-$4,400.50";
                  var number = Number(currency.replace(/[^0-9.-]+/g,""));






                  share|improve this answer













                  please try below code



                  var currency = "-$4,400.50";
                  var number = Number(currency.replace(/[^0-9.-]+/g,""));







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 11:05









                  jigneshjignesh

                  1




                  1























                      -1














                      First get balance in string, and then replace $ and , with '' (blank quotes) then use int.parse.



                      For example :



                      string a = balance
                      a = a.replace("$","");
                      a = a.replace(",","");
                      int balance = int.parse(a);





                      share|improve this answer


























                      • im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc

                        – no name
                        Mar 22 '17 at 4:41
















                      -1














                      First get balance in string, and then replace $ and , with '' (blank quotes) then use int.parse.



                      For example :



                      string a = balance
                      a = a.replace("$","");
                      a = a.replace(",","");
                      int balance = int.parse(a);





                      share|improve this answer


























                      • im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc

                        – no name
                        Mar 22 '17 at 4:41














                      -1












                      -1








                      -1







                      First get balance in string, and then replace $ and , with '' (blank quotes) then use int.parse.



                      For example :



                      string a = balance
                      a = a.replace("$","");
                      a = a.replace(",","");
                      int balance = int.parse(a);





                      share|improve this answer















                      First get balance in string, and then replace $ and , with '' (blank quotes) then use int.parse.



                      For example :



                      string a = balance
                      a = a.replace("$","");
                      a = a.replace(",","");
                      int balance = int.parse(a);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 22 '17 at 6:56









                      Deep Kakkar

                      3,91611750




                      3,91611750










                      answered Mar 22 '17 at 4:30









                      jigneshjignesh

                      1




                      1













                      • im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc

                        – no name
                        Mar 22 '17 at 4:41



















                      • im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc

                        – no name
                        Mar 22 '17 at 4:41

















                      im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc

                      – no name
                      Mar 22 '17 at 4:41





                      im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc

                      – no name
                      Mar 22 '17 at 4:41


















                      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%2f42942580%2fnumeric-comparisons-such-as-greater-than-on-a-currency-amount-stored-as-a-string%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

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter