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;
}
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
add a comment |
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
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
add a comment |
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
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
mongodb mongodb-query
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
You could use a regular expression like:
db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });
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
add a comment |
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.
add a comment |
Number(currency.replace(/[^0-9.-]+/g,""));
var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));
– jignesh
Jan 3 at 11:02
add a comment |
please try below code
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
add a comment |
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);
im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc
– no name
Mar 22 '17 at 4:41
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You could use a regular expression like:
db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });
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
add a comment |
You could use a regular expression like:
db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });
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
add a comment |
You could use a regular expression like:
db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });
You could use a regular expression like:
db.users.find({ balance: { $regex: /^$[1-9][0-9,]{3,}/ } });
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 22 '17 at 15:35
Vince BowdrenVince Bowdren
4,43721841
4,43721841
add a comment |
add a comment |
Number(currency.replace(/[^0-9.-]+/g,""));
var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));
– jignesh
Jan 3 at 11:02
add a comment |
Number(currency.replace(/[^0-9.-]+/g,""));
var currency = "-$4,400.50"; var number = Number(currency.replace(/[^0-9.-]+/g,""));
– jignesh
Jan 3 at 11:02
add a comment |
Number(currency.replace(/[^0-9.-]+/g,""));
Number(currency.replace(/[^0-9.-]+/g,""));
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
add a comment |
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
add a comment |
please try below code
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
add a comment |
please try below code
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
add a comment |
please try below code
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
please try below code
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
answered Jan 3 at 11:05
jigneshjignesh
1
1
add a comment |
add a comment |
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);
im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc
– no name
Mar 22 '17 at 4:41
add a comment |
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);
im not using a proper lang, im using commands: gyazo.com/e99fa8215cd8b90d19d2fab7dd4680fc
– no name
Mar 22 '17 at 4:41
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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