MongoDb/ Mongoose Aggregation Issue With Matching Date
I am trying to use Mongoose/ MongoDb's aggregate function to match documents based on the date. While my aggregate function matches documents by other filters, It is simply not working based on date. I am sharing my code below.
"$match":
"createdAt": "$lt": new Date()
I was trying a variety of different dates, Starting with the ISO date stored in my mongoDb database, But none of them seem to work. I have no idea what is the reason for this issue. While I had searched online extensively, I did not find anything conclusive. The same query executed over the mongoDb shell works fine, Leading me to believe that this may be an issue with Mongoose and the way it casts the Date.
This is the data I was testing with
"_id" : ObjectId("5be7fc909625592852d422b4"),
"time" : ISODate("2018-11-11T09:55:28.704Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-11-11T09:55:28.718Z"),
"updatedAt" : ISODate("2018-11-11T09:55:28.718Z"),
"_id" : ObjectId("5be807902b789e5c3c05dc5e"),
"time" : ISODate("2018-11-11T10:42:24.410Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-10-09T18:30:00Z"),
"updatedAt" : ISODate("2018-10-09T18:30:00Z"),
I am stuck, With no idea how to proceed ahead. I have tried everything from my understanding but nothing seems to work. I hope I can get some help on this issue.
Much appreciated, Thanks.
mongodb date mongoose aggregate iso
add a comment |
I am trying to use Mongoose/ MongoDb's aggregate function to match documents based on the date. While my aggregate function matches documents by other filters, It is simply not working based on date. I am sharing my code below.
"$match":
"createdAt": "$lt": new Date()
I was trying a variety of different dates, Starting with the ISO date stored in my mongoDb database, But none of them seem to work. I have no idea what is the reason for this issue. While I had searched online extensively, I did not find anything conclusive. The same query executed over the mongoDb shell works fine, Leading me to believe that this may be an issue with Mongoose and the way it casts the Date.
This is the data I was testing with
"_id" : ObjectId("5be7fc909625592852d422b4"),
"time" : ISODate("2018-11-11T09:55:28.704Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-11-11T09:55:28.718Z"),
"updatedAt" : ISODate("2018-11-11T09:55:28.718Z"),
"_id" : ObjectId("5be807902b789e5c3c05dc5e"),
"time" : ISODate("2018-11-11T10:42:24.410Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-10-09T18:30:00Z"),
"updatedAt" : ISODate("2018-10-09T18:30:00Z"),
I am stuck, With no idea how to proceed ahead. I have tried everything from my understanding but nothing seems to work. I hope I can get some help on this issue.
Much appreciated, Thanks.
mongodb date mongoose aggregate iso
please provide your data sample
– matthPen
Nov 12 '18 at 14:27
Please check the edit
– Damin Han
Nov 12 '18 at 14:36
Welcome to SO. Please calrify "does not seem to work". Are you getting an error? Too many documents? or none?
– mbuechmann
Nov 12 '18 at 15:20
My bad. What I meant 'does not seem to work' was that the aggregate function returned no documents ie matched no documents when I ran the query against the date.
– Damin Han
Nov 13 '18 at 14:37
add a comment |
I am trying to use Mongoose/ MongoDb's aggregate function to match documents based on the date. While my aggregate function matches documents by other filters, It is simply not working based on date. I am sharing my code below.
"$match":
"createdAt": "$lt": new Date()
I was trying a variety of different dates, Starting with the ISO date stored in my mongoDb database, But none of them seem to work. I have no idea what is the reason for this issue. While I had searched online extensively, I did not find anything conclusive. The same query executed over the mongoDb shell works fine, Leading me to believe that this may be an issue with Mongoose and the way it casts the Date.
This is the data I was testing with
"_id" : ObjectId("5be7fc909625592852d422b4"),
"time" : ISODate("2018-11-11T09:55:28.704Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-11-11T09:55:28.718Z"),
"updatedAt" : ISODate("2018-11-11T09:55:28.718Z"),
"_id" : ObjectId("5be807902b789e5c3c05dc5e"),
"time" : ISODate("2018-11-11T10:42:24.410Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-10-09T18:30:00Z"),
"updatedAt" : ISODate("2018-10-09T18:30:00Z"),
I am stuck, With no idea how to proceed ahead. I have tried everything from my understanding but nothing seems to work. I hope I can get some help on this issue.
Much appreciated, Thanks.
mongodb date mongoose aggregate iso
I am trying to use Mongoose/ MongoDb's aggregate function to match documents based on the date. While my aggregate function matches documents by other filters, It is simply not working based on date. I am sharing my code below.
"$match":
"createdAt": "$lt": new Date()
I was trying a variety of different dates, Starting with the ISO date stored in my mongoDb database, But none of them seem to work. I have no idea what is the reason for this issue. While I had searched online extensively, I did not find anything conclusive. The same query executed over the mongoDb shell works fine, Leading me to believe that this may be an issue with Mongoose and the way it casts the Date.
This is the data I was testing with
"_id" : ObjectId("5be7fc909625592852d422b4"),
"time" : ISODate("2018-11-11T09:55:28.704Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-11-11T09:55:28.718Z"),
"updatedAt" : ISODate("2018-11-11T09:55:28.718Z"),
"_id" : ObjectId("5be807902b789e5c3c05dc5e"),
"time" : ISODate("2018-11-11T10:42:24.410Z"),
"receiver" : ObjectId("5be5d727d3f24e06b144e229"),
"createdAt" : ISODate("2018-10-09T18:30:00Z"),
"updatedAt" : ISODate("2018-10-09T18:30:00Z"),
I am stuck, With no idea how to proceed ahead. I have tried everything from my understanding but nothing seems to work. I hope I can get some help on this issue.
Much appreciated, Thanks.
mongodb date mongoose aggregate iso
mongodb date mongoose aggregate iso
edited Nov 12 '18 at 14:36
Damin Han
asked Nov 12 '18 at 13:47
Damin HanDamin Han
11
11
please provide your data sample
– matthPen
Nov 12 '18 at 14:27
Please check the edit
– Damin Han
Nov 12 '18 at 14:36
Welcome to SO. Please calrify "does not seem to work". Are you getting an error? Too many documents? or none?
– mbuechmann
Nov 12 '18 at 15:20
My bad. What I meant 'does not seem to work' was that the aggregate function returned no documents ie matched no documents when I ran the query against the date.
– Damin Han
Nov 13 '18 at 14:37
add a comment |
please provide your data sample
– matthPen
Nov 12 '18 at 14:27
Please check the edit
– Damin Han
Nov 12 '18 at 14:36
Welcome to SO. Please calrify "does not seem to work". Are you getting an error? Too many documents? or none?
– mbuechmann
Nov 12 '18 at 15:20
My bad. What I meant 'does not seem to work' was that the aggregate function returned no documents ie matched no documents when I ran the query against the date.
– Damin Han
Nov 13 '18 at 14:37
please provide your data sample
– matthPen
Nov 12 '18 at 14:27
please provide your data sample
– matthPen
Nov 12 '18 at 14:27
Please check the edit
– Damin Han
Nov 12 '18 at 14:36
Please check the edit
– Damin Han
Nov 12 '18 at 14:36
Welcome to SO. Please calrify "does not seem to work". Are you getting an error? Too many documents? or none?
– mbuechmann
Nov 12 '18 at 15:20
Welcome to SO. Please calrify "does not seem to work". Are you getting an error? Too many documents? or none?
– mbuechmann
Nov 12 '18 at 15:20
My bad. What I meant 'does not seem to work' was that the aggregate function returned no documents ie matched no documents when I ran the query against the date.
– Damin Han
Nov 13 '18 at 14:37
My bad. What I meant 'does not seem to work' was that the aggregate function returned no documents ie matched no documents when I ran the query against the date.
– Damin Han
Nov 13 '18 at 14:37
add a comment |
1 Answer
1
active
oldest
votes
You are possibly facing a timezone problem. I just tried the call of new Date().getTimezoneOffset()
inside node
and inside mongo
. Both return different results (node returns -60
, mongo shell returns 0
on my machine).
You can remove the timezone offset with the following code:
let d = new Date()
var offset = d.getTimezoneOffset() * 60000 // This converts the given minutes to milliseconds.
var utc = d.getTime() + offset
var nd = new Date(utc)
"$match":
"createdAt": "$lt": nd
When you convert the time you should now get the expected results.
This is sensible as MongoDB uses UTC as time format. The conversion might seem a bit cumbersome in vanilla JavaScript. You could alternatively use a library like moment.js
.
I was trying this yesterday, But I was still unable to match the documents using the code you shared. I did get an idea from what you said - And tested my code on the node terminal using my mongoose models. Surprisingly I was able to match the condition here, Using the exact same query I shared. I suspect this is something wrong from my side, Maybe I am casting the date wrong ? I'll have to keep trying - But thanks for your help @mbuechmann
– Damin Han
Nov 14 '18 at 5:59
@DaminHan you can always calltoString()
on any date value. Perhaps in this way you can find the relevant discrepancies.
– mbuechmann
Nov 14 '18 at 6:17
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%2f53263541%2fmongodb-mongoose-aggregation-issue-with-matching-date%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
You are possibly facing a timezone problem. I just tried the call of new Date().getTimezoneOffset()
inside node
and inside mongo
. Both return different results (node returns -60
, mongo shell returns 0
on my machine).
You can remove the timezone offset with the following code:
let d = new Date()
var offset = d.getTimezoneOffset() * 60000 // This converts the given minutes to milliseconds.
var utc = d.getTime() + offset
var nd = new Date(utc)
"$match":
"createdAt": "$lt": nd
When you convert the time you should now get the expected results.
This is sensible as MongoDB uses UTC as time format. The conversion might seem a bit cumbersome in vanilla JavaScript. You could alternatively use a library like moment.js
.
I was trying this yesterday, But I was still unable to match the documents using the code you shared. I did get an idea from what you said - And tested my code on the node terminal using my mongoose models. Surprisingly I was able to match the condition here, Using the exact same query I shared. I suspect this is something wrong from my side, Maybe I am casting the date wrong ? I'll have to keep trying - But thanks for your help @mbuechmann
– Damin Han
Nov 14 '18 at 5:59
@DaminHan you can always calltoString()
on any date value. Perhaps in this way you can find the relevant discrepancies.
– mbuechmann
Nov 14 '18 at 6:17
add a comment |
You are possibly facing a timezone problem. I just tried the call of new Date().getTimezoneOffset()
inside node
and inside mongo
. Both return different results (node returns -60
, mongo shell returns 0
on my machine).
You can remove the timezone offset with the following code:
let d = new Date()
var offset = d.getTimezoneOffset() * 60000 // This converts the given minutes to milliseconds.
var utc = d.getTime() + offset
var nd = new Date(utc)
"$match":
"createdAt": "$lt": nd
When you convert the time you should now get the expected results.
This is sensible as MongoDB uses UTC as time format. The conversion might seem a bit cumbersome in vanilla JavaScript. You could alternatively use a library like moment.js
.
I was trying this yesterday, But I was still unable to match the documents using the code you shared. I did get an idea from what you said - And tested my code on the node terminal using my mongoose models. Surprisingly I was able to match the condition here, Using the exact same query I shared. I suspect this is something wrong from my side, Maybe I am casting the date wrong ? I'll have to keep trying - But thanks for your help @mbuechmann
– Damin Han
Nov 14 '18 at 5:59
@DaminHan you can always calltoString()
on any date value. Perhaps in this way you can find the relevant discrepancies.
– mbuechmann
Nov 14 '18 at 6:17
add a comment |
You are possibly facing a timezone problem. I just tried the call of new Date().getTimezoneOffset()
inside node
and inside mongo
. Both return different results (node returns -60
, mongo shell returns 0
on my machine).
You can remove the timezone offset with the following code:
let d = new Date()
var offset = d.getTimezoneOffset() * 60000 // This converts the given minutes to milliseconds.
var utc = d.getTime() + offset
var nd = new Date(utc)
"$match":
"createdAt": "$lt": nd
When you convert the time you should now get the expected results.
This is sensible as MongoDB uses UTC as time format. The conversion might seem a bit cumbersome in vanilla JavaScript. You could alternatively use a library like moment.js
.
You are possibly facing a timezone problem. I just tried the call of new Date().getTimezoneOffset()
inside node
and inside mongo
. Both return different results (node returns -60
, mongo shell returns 0
on my machine).
You can remove the timezone offset with the following code:
let d = new Date()
var offset = d.getTimezoneOffset() * 60000 // This converts the given minutes to milliseconds.
var utc = d.getTime() + offset
var nd = new Date(utc)
"$match":
"createdAt": "$lt": nd
When you convert the time you should now get the expected results.
This is sensible as MongoDB uses UTC as time format. The conversion might seem a bit cumbersome in vanilla JavaScript. You could alternatively use a library like moment.js
.
answered Nov 12 '18 at 15:33
mbuechmannmbuechmann
2,82121325
2,82121325
I was trying this yesterday, But I was still unable to match the documents using the code you shared. I did get an idea from what you said - And tested my code on the node terminal using my mongoose models. Surprisingly I was able to match the condition here, Using the exact same query I shared. I suspect this is something wrong from my side, Maybe I am casting the date wrong ? I'll have to keep trying - But thanks for your help @mbuechmann
– Damin Han
Nov 14 '18 at 5:59
@DaminHan you can always calltoString()
on any date value. Perhaps in this way you can find the relevant discrepancies.
– mbuechmann
Nov 14 '18 at 6:17
add a comment |
I was trying this yesterday, But I was still unable to match the documents using the code you shared. I did get an idea from what you said - And tested my code on the node terminal using my mongoose models. Surprisingly I was able to match the condition here, Using the exact same query I shared. I suspect this is something wrong from my side, Maybe I am casting the date wrong ? I'll have to keep trying - But thanks for your help @mbuechmann
– Damin Han
Nov 14 '18 at 5:59
@DaminHan you can always calltoString()
on any date value. Perhaps in this way you can find the relevant discrepancies.
– mbuechmann
Nov 14 '18 at 6:17
I was trying this yesterday, But I was still unable to match the documents using the code you shared. I did get an idea from what you said - And tested my code on the node terminal using my mongoose models. Surprisingly I was able to match the condition here, Using the exact same query I shared. I suspect this is something wrong from my side, Maybe I am casting the date wrong ? I'll have to keep trying - But thanks for your help @mbuechmann
– Damin Han
Nov 14 '18 at 5:59
I was trying this yesterday, But I was still unable to match the documents using the code you shared. I did get an idea from what you said - And tested my code on the node terminal using my mongoose models. Surprisingly I was able to match the condition here, Using the exact same query I shared. I suspect this is something wrong from my side, Maybe I am casting the date wrong ? I'll have to keep trying - But thanks for your help @mbuechmann
– Damin Han
Nov 14 '18 at 5:59
@DaminHan you can always call
toString()
on any date value. Perhaps in this way you can find the relevant discrepancies.– mbuechmann
Nov 14 '18 at 6:17
@DaminHan you can always call
toString()
on any date value. Perhaps in this way you can find the relevant discrepancies.– mbuechmann
Nov 14 '18 at 6:17
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%2f53263541%2fmongodb-mongoose-aggregation-issue-with-matching-date%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
please provide your data sample
– matthPen
Nov 12 '18 at 14:27
Please check the edit
– Damin Han
Nov 12 '18 at 14:36
Welcome to SO. Please calrify "does not seem to work". Are you getting an error? Too many documents? or none?
– mbuechmann
Nov 12 '18 at 15:20
My bad. What I meant 'does not seem to work' was that the aggregate function returned no documents ie matched no documents when I ran the query against the date.
– Damin Han
Nov 13 '18 at 14:37