mongodb Querying for a Date Range when date is saved as string










3















I'm saving data into the bongo as bulk insert. The data that's an array of JSON object contain date, numeric, alphanumeric data all saved as string.



Sample Data



[
"CARDNO": "661",
"HOLDERNO": "661",
"HOLDERNAME": "S",
"IODATE": "4/1/2012",
"IOTIME": "00:03:27",
"IOGATENO": "01-3",
"IOGATENAME": "FWork",
"IOSTATUS": "Entry",
"DEPARTMENTNO": "1",
"UPDATE_STATUS": "1"
,
"CARDNO": "711",
"HOLDERNO": "711",
"HOLDERNAME": "P",
"IODATE": "4/1/2012",
"IOTIME": "04:35:33",
"IOGATENO": "01-7",
"IOGATENAME": "FDWork",
"IOSTATUS": "Exit",
"DEPARTMENTNO": "3",
"UPDATE_STATUS": "1"
]


My Query



var start = new Date(2012, 4, 15);
var end = new Date(2012, 4, 1);



collection.find(
"IODATE":
$gte: start,
$lt: end

).toArray(function (err, data)
if (err)
console.log(err);
else
console.log(data.length)

//res.send(data.length);
res.send(JSON.stringify(data));
);


It's not returning result, I think it is because the value of "IODATE" is in string inside db.



How to work around this issue? I may need to do bulk insert since the data can be of 200 million or so.










share|improve this question






















  • I would personally redo your schema, this is a complete flaw in your schema especially if you need to do more advanced things with the dates

    – Sammaye
    Feb 21 '14 at 12:07











  • @Sammaye I agree with you. but in my case its not a good option to change schema; even to say its impossible.

    – Okky
    Feb 23 '14 at 6:33















3















I'm saving data into the bongo as bulk insert. The data that's an array of JSON object contain date, numeric, alphanumeric data all saved as string.



Sample Data



[
"CARDNO": "661",
"HOLDERNO": "661",
"HOLDERNAME": "S",
"IODATE": "4/1/2012",
"IOTIME": "00:03:27",
"IOGATENO": "01-3",
"IOGATENAME": "FWork",
"IOSTATUS": "Entry",
"DEPARTMENTNO": "1",
"UPDATE_STATUS": "1"
,
"CARDNO": "711",
"HOLDERNO": "711",
"HOLDERNAME": "P",
"IODATE": "4/1/2012",
"IOTIME": "04:35:33",
"IOGATENO": "01-7",
"IOGATENAME": "FDWork",
"IOSTATUS": "Exit",
"DEPARTMENTNO": "3",
"UPDATE_STATUS": "1"
]


My Query



var start = new Date(2012, 4, 15);
var end = new Date(2012, 4, 1);



collection.find(
"IODATE":
$gte: start,
$lt: end

).toArray(function (err, data)
if (err)
console.log(err);
else
console.log(data.length)

//res.send(data.length);
res.send(JSON.stringify(data));
);


It's not returning result, I think it is because the value of "IODATE" is in string inside db.



How to work around this issue? I may need to do bulk insert since the data can be of 200 million or so.










share|improve this question






















  • I would personally redo your schema, this is a complete flaw in your schema especially if you need to do more advanced things with the dates

    – Sammaye
    Feb 21 '14 at 12:07











  • @Sammaye I agree with you. but in my case its not a good option to change schema; even to say its impossible.

    – Okky
    Feb 23 '14 at 6:33













3












3








3


1






I'm saving data into the bongo as bulk insert. The data that's an array of JSON object contain date, numeric, alphanumeric data all saved as string.



Sample Data



[
"CARDNO": "661",
"HOLDERNO": "661",
"HOLDERNAME": "S",
"IODATE": "4/1/2012",
"IOTIME": "00:03:27",
"IOGATENO": "01-3",
"IOGATENAME": "FWork",
"IOSTATUS": "Entry",
"DEPARTMENTNO": "1",
"UPDATE_STATUS": "1"
,
"CARDNO": "711",
"HOLDERNO": "711",
"HOLDERNAME": "P",
"IODATE": "4/1/2012",
"IOTIME": "04:35:33",
"IOGATENO": "01-7",
"IOGATENAME": "FDWork",
"IOSTATUS": "Exit",
"DEPARTMENTNO": "3",
"UPDATE_STATUS": "1"
]


My Query



var start = new Date(2012, 4, 15);
var end = new Date(2012, 4, 1);



collection.find(
"IODATE":
$gte: start,
$lt: end

).toArray(function (err, data)
if (err)
console.log(err);
else
console.log(data.length)

//res.send(data.length);
res.send(JSON.stringify(data));
);


It's not returning result, I think it is because the value of "IODATE" is in string inside db.



How to work around this issue? I may need to do bulk insert since the data can be of 200 million or so.










share|improve this question














I'm saving data into the bongo as bulk insert. The data that's an array of JSON object contain date, numeric, alphanumeric data all saved as string.



Sample Data



[
"CARDNO": "661",
"HOLDERNO": "661",
"HOLDERNAME": "S",
"IODATE": "4/1/2012",
"IOTIME": "00:03:27",
"IOGATENO": "01-3",
"IOGATENAME": "FWork",
"IOSTATUS": "Entry",
"DEPARTMENTNO": "1",
"UPDATE_STATUS": "1"
,
"CARDNO": "711",
"HOLDERNO": "711",
"HOLDERNAME": "P",
"IODATE": "4/1/2012",
"IOTIME": "04:35:33",
"IOGATENO": "01-7",
"IOGATENAME": "FDWork",
"IOSTATUS": "Exit",
"DEPARTMENTNO": "3",
"UPDATE_STATUS": "1"
]


My Query



var start = new Date(2012, 4, 15);
var end = new Date(2012, 4, 1);



collection.find(
"IODATE":
$gte: start,
$lt: end

).toArray(function (err, data)
if (err)
console.log(err);
else
console.log(data.length)

//res.send(data.length);
res.send(JSON.stringify(data));
);


It's not returning result, I think it is because the value of "IODATE" is in string inside db.



How to work around this issue? I may need to do bulk insert since the data can be of 200 million or so.







mongodb date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 21 '14 at 11:39









OkkyOkky

5,605853112




5,605853112












  • I would personally redo your schema, this is a complete flaw in your schema especially if you need to do more advanced things with the dates

    – Sammaye
    Feb 21 '14 at 12:07











  • @Sammaye I agree with you. but in my case its not a good option to change schema; even to say its impossible.

    – Okky
    Feb 23 '14 at 6:33

















  • I would personally redo your schema, this is a complete flaw in your schema especially if you need to do more advanced things with the dates

    – Sammaye
    Feb 21 '14 at 12:07











  • @Sammaye I agree with you. but in my case its not a good option to change schema; even to say its impossible.

    – Okky
    Feb 23 '14 at 6:33
















I would personally redo your schema, this is a complete flaw in your schema especially if you need to do more advanced things with the dates

– Sammaye
Feb 21 '14 at 12:07





I would personally redo your schema, this is a complete flaw in your schema especially if you need to do more advanced things with the dates

– Sammaye
Feb 21 '14 at 12:07













@Sammaye I agree with you. but in my case its not a good option to change schema; even to say its impossible.

– Okky
Feb 23 '14 at 6:33





@Sammaye I agree with you. but in my case its not a good option to change schema; even to say its impossible.

– Okky
Feb 23 '14 at 6:33












1 Answer
1






active

oldest

votes


















3














One last try at this, because you don't have a good record of accepting good advice.



Your date formats as they stand are going to bite you. Even where trying to work around them. Here are the problems:



  • The format is not lexical. Which means that even with a string comparison operators like $gte, $lte are just not going to work. A lexical date would be "2012-01-04" in "yyyy-mm-dd" format. That would work with the operators.


  • You could look at $substr (and it's complete lack of documentation, search on SO for real usage) within aggregate but your date format is lacking the double digit form of day and month ie "04/01/2012", so that is going to blow up the positional nature of the operator. Also you would have to transform before any $match which means you blow up any chance of reducing your pipeline input, so you are stuck with not being able to filter your large resultset by date.


It's a horrible case, but there really is no other practical solution to the large data problem here than to convert your dates. Strings in the form that you have just do not cut it. Either, in order of preference convert to:



  • BSON date

  • epoch timestamp as long (whatever)

  • Lexical string representation (as described)

Your main case seems to be filtering, so updating the dataset is the only pratical alternative. Otherwise you are stuck with "paging" results and doing a lot of manual work, that could otherwise be done server side.






share|improve this answer























  • K :). I will try to do something to convert my dates.

    – Okky
    Feb 24 '14 at 8:59











  • @SreekeshOkky Sorry, I know it's a tough pill to swallow. But if you want to query you'd be in a lot of trouble without it. Beats iterating.

    – Neil Lunn
    Feb 24 '14 at 9:02











  • True :) Thing that bothers me is the bulk amount of data. It will take time if I'm gonna iterate through each and every record and convert it into date. Same goes with my numeric also.

    – Okky
    Feb 24 '14 at 9:53






  • 1





    @SreekeshOkky It's also true that just because an answer didn't provide a "golden rainbow" that solved all your problems, that it wasn't valid and informative, and gave you a direction to move on. The common practice when you receive an answer that gave you such direction is to accept it. Hence how I opened the starting phrase in my response. Someone just gave you knowledge. Be a fair citizen and repay the favour with an accept (and probably, fairly and up-vote). Your own rep came from where?

    – Neil Lunn
    Feb 24 '14 at 12:25










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%2f21933473%2fmongodb-querying-for-a-date-range-when-date-is-saved-as-string%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









3














One last try at this, because you don't have a good record of accepting good advice.



Your date formats as they stand are going to bite you. Even where trying to work around them. Here are the problems:



  • The format is not lexical. Which means that even with a string comparison operators like $gte, $lte are just not going to work. A lexical date would be "2012-01-04" in "yyyy-mm-dd" format. That would work with the operators.


  • You could look at $substr (and it's complete lack of documentation, search on SO for real usage) within aggregate but your date format is lacking the double digit form of day and month ie "04/01/2012", so that is going to blow up the positional nature of the operator. Also you would have to transform before any $match which means you blow up any chance of reducing your pipeline input, so you are stuck with not being able to filter your large resultset by date.


It's a horrible case, but there really is no other practical solution to the large data problem here than to convert your dates. Strings in the form that you have just do not cut it. Either, in order of preference convert to:



  • BSON date

  • epoch timestamp as long (whatever)

  • Lexical string representation (as described)

Your main case seems to be filtering, so updating the dataset is the only pratical alternative. Otherwise you are stuck with "paging" results and doing a lot of manual work, that could otherwise be done server side.






share|improve this answer























  • K :). I will try to do something to convert my dates.

    – Okky
    Feb 24 '14 at 8:59











  • @SreekeshOkky Sorry, I know it's a tough pill to swallow. But if you want to query you'd be in a lot of trouble without it. Beats iterating.

    – Neil Lunn
    Feb 24 '14 at 9:02











  • True :) Thing that bothers me is the bulk amount of data. It will take time if I'm gonna iterate through each and every record and convert it into date. Same goes with my numeric also.

    – Okky
    Feb 24 '14 at 9:53






  • 1





    @SreekeshOkky It's also true that just because an answer didn't provide a "golden rainbow" that solved all your problems, that it wasn't valid and informative, and gave you a direction to move on. The common practice when you receive an answer that gave you such direction is to accept it. Hence how I opened the starting phrase in my response. Someone just gave you knowledge. Be a fair citizen and repay the favour with an accept (and probably, fairly and up-vote). Your own rep came from where?

    – Neil Lunn
    Feb 24 '14 at 12:25















3














One last try at this, because you don't have a good record of accepting good advice.



Your date formats as they stand are going to bite you. Even where trying to work around them. Here are the problems:



  • The format is not lexical. Which means that even with a string comparison operators like $gte, $lte are just not going to work. A lexical date would be "2012-01-04" in "yyyy-mm-dd" format. That would work with the operators.


  • You could look at $substr (and it's complete lack of documentation, search on SO for real usage) within aggregate but your date format is lacking the double digit form of day and month ie "04/01/2012", so that is going to blow up the positional nature of the operator. Also you would have to transform before any $match which means you blow up any chance of reducing your pipeline input, so you are stuck with not being able to filter your large resultset by date.


It's a horrible case, but there really is no other practical solution to the large data problem here than to convert your dates. Strings in the form that you have just do not cut it. Either, in order of preference convert to:



  • BSON date

  • epoch timestamp as long (whatever)

  • Lexical string representation (as described)

Your main case seems to be filtering, so updating the dataset is the only pratical alternative. Otherwise you are stuck with "paging" results and doing a lot of manual work, that could otherwise be done server side.






share|improve this answer























  • K :). I will try to do something to convert my dates.

    – Okky
    Feb 24 '14 at 8:59











  • @SreekeshOkky Sorry, I know it's a tough pill to swallow. But if you want to query you'd be in a lot of trouble without it. Beats iterating.

    – Neil Lunn
    Feb 24 '14 at 9:02











  • True :) Thing that bothers me is the bulk amount of data. It will take time if I'm gonna iterate through each and every record and convert it into date. Same goes with my numeric also.

    – Okky
    Feb 24 '14 at 9:53






  • 1





    @SreekeshOkky It's also true that just because an answer didn't provide a "golden rainbow" that solved all your problems, that it wasn't valid and informative, and gave you a direction to move on. The common practice when you receive an answer that gave you such direction is to accept it. Hence how I opened the starting phrase in my response. Someone just gave you knowledge. Be a fair citizen and repay the favour with an accept (and probably, fairly and up-vote). Your own rep came from where?

    – Neil Lunn
    Feb 24 '14 at 12:25













3












3








3







One last try at this, because you don't have a good record of accepting good advice.



Your date formats as they stand are going to bite you. Even where trying to work around them. Here are the problems:



  • The format is not lexical. Which means that even with a string comparison operators like $gte, $lte are just not going to work. A lexical date would be "2012-01-04" in "yyyy-mm-dd" format. That would work with the operators.


  • You could look at $substr (and it's complete lack of documentation, search on SO for real usage) within aggregate but your date format is lacking the double digit form of day and month ie "04/01/2012", so that is going to blow up the positional nature of the operator. Also you would have to transform before any $match which means you blow up any chance of reducing your pipeline input, so you are stuck with not being able to filter your large resultset by date.


It's a horrible case, but there really is no other practical solution to the large data problem here than to convert your dates. Strings in the form that you have just do not cut it. Either, in order of preference convert to:



  • BSON date

  • epoch timestamp as long (whatever)

  • Lexical string representation (as described)

Your main case seems to be filtering, so updating the dataset is the only pratical alternative. Otherwise you are stuck with "paging" results and doing a lot of manual work, that could otherwise be done server side.






share|improve this answer













One last try at this, because you don't have a good record of accepting good advice.



Your date formats as they stand are going to bite you. Even where trying to work around them. Here are the problems:



  • The format is not lexical. Which means that even with a string comparison operators like $gte, $lte are just not going to work. A lexical date would be "2012-01-04" in "yyyy-mm-dd" format. That would work with the operators.


  • You could look at $substr (and it's complete lack of documentation, search on SO for real usage) within aggregate but your date format is lacking the double digit form of day and month ie "04/01/2012", so that is going to blow up the positional nature of the operator. Also you would have to transform before any $match which means you blow up any chance of reducing your pipeline input, so you are stuck with not being able to filter your large resultset by date.


It's a horrible case, but there really is no other practical solution to the large data problem here than to convert your dates. Strings in the form that you have just do not cut it. Either, in order of preference convert to:



  • BSON date

  • epoch timestamp as long (whatever)

  • Lexical string representation (as described)

Your main case seems to be filtering, so updating the dataset is the only pratical alternative. Otherwise you are stuck with "paging" results and doing a lot of manual work, that could otherwise be done server side.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 21 '14 at 12:39









Neil LunnNeil Lunn

98.5k23174184




98.5k23174184












  • K :). I will try to do something to convert my dates.

    – Okky
    Feb 24 '14 at 8:59











  • @SreekeshOkky Sorry, I know it's a tough pill to swallow. But if you want to query you'd be in a lot of trouble without it. Beats iterating.

    – Neil Lunn
    Feb 24 '14 at 9:02











  • True :) Thing that bothers me is the bulk amount of data. It will take time if I'm gonna iterate through each and every record and convert it into date. Same goes with my numeric also.

    – Okky
    Feb 24 '14 at 9:53






  • 1





    @SreekeshOkky It's also true that just because an answer didn't provide a "golden rainbow" that solved all your problems, that it wasn't valid and informative, and gave you a direction to move on. The common practice when you receive an answer that gave you such direction is to accept it. Hence how I opened the starting phrase in my response. Someone just gave you knowledge. Be a fair citizen and repay the favour with an accept (and probably, fairly and up-vote). Your own rep came from where?

    – Neil Lunn
    Feb 24 '14 at 12:25

















  • K :). I will try to do something to convert my dates.

    – Okky
    Feb 24 '14 at 8:59











  • @SreekeshOkky Sorry, I know it's a tough pill to swallow. But if you want to query you'd be in a lot of trouble without it. Beats iterating.

    – Neil Lunn
    Feb 24 '14 at 9:02











  • True :) Thing that bothers me is the bulk amount of data. It will take time if I'm gonna iterate through each and every record and convert it into date. Same goes with my numeric also.

    – Okky
    Feb 24 '14 at 9:53






  • 1





    @SreekeshOkky It's also true that just because an answer didn't provide a "golden rainbow" that solved all your problems, that it wasn't valid and informative, and gave you a direction to move on. The common practice when you receive an answer that gave you such direction is to accept it. Hence how I opened the starting phrase in my response. Someone just gave you knowledge. Be a fair citizen and repay the favour with an accept (and probably, fairly and up-vote). Your own rep came from where?

    – Neil Lunn
    Feb 24 '14 at 12:25
















K :). I will try to do something to convert my dates.

– Okky
Feb 24 '14 at 8:59





K :). I will try to do something to convert my dates.

– Okky
Feb 24 '14 at 8:59













@SreekeshOkky Sorry, I know it's a tough pill to swallow. But if you want to query you'd be in a lot of trouble without it. Beats iterating.

– Neil Lunn
Feb 24 '14 at 9:02





@SreekeshOkky Sorry, I know it's a tough pill to swallow. But if you want to query you'd be in a lot of trouble without it. Beats iterating.

– Neil Lunn
Feb 24 '14 at 9:02













True :) Thing that bothers me is the bulk amount of data. It will take time if I'm gonna iterate through each and every record and convert it into date. Same goes with my numeric also.

– Okky
Feb 24 '14 at 9:53





True :) Thing that bothers me is the bulk amount of data. It will take time if I'm gonna iterate through each and every record and convert it into date. Same goes with my numeric also.

– Okky
Feb 24 '14 at 9:53




1




1





@SreekeshOkky It's also true that just because an answer didn't provide a "golden rainbow" that solved all your problems, that it wasn't valid and informative, and gave you a direction to move on. The common practice when you receive an answer that gave you such direction is to accept it. Hence how I opened the starting phrase in my response. Someone just gave you knowledge. Be a fair citizen and repay the favour with an accept (and probably, fairly and up-vote). Your own rep came from where?

– Neil Lunn
Feb 24 '14 at 12:25





@SreekeshOkky It's also true that just because an answer didn't provide a "golden rainbow" that solved all your problems, that it wasn't valid and informative, and gave you a direction to move on. The common practice when you receive an answer that gave you such direction is to accept it. Hence how I opened the starting phrase in my response. Someone just gave you knowledge. Be a fair citizen and repay the favour with an accept (and probably, fairly and up-vote). Your own rep came from where?

– Neil Lunn
Feb 24 '14 at 12:25



















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%2f21933473%2fmongodb-querying-for-a-date-range-when-date-is-saved-as-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

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo