Mongoose $gte Date












0














Why is mongo client returning an answer for the query and Mongoose is not.



db.dates.find({"date_out" : { $gte : ISODate("2018-11-12T00:00:00.000Z") }}).pretty()
{
"_id" : ObjectId("5be8a9eeda7bbc1fc40c1fa1"),
"user" : "some.user@example.com",
"date_in" : ISODate("2018-11-11T22:15:10.095Z"),
"date_out" : ISODate("2018-11-14T22:00:00Z"),
"userId" : "5be5a96e6db7be0568ea6e47"
}


But when I try to do the same query in Mongoose it doesn't return anything



import Dates from "../models/Date";
import moment from 'moment';
const today = moment().startOf('day').toDate(); // 2018-11-11T22:00:00.000Z
const query = { date_out : { $gte : today }};

router.get("/unavailable", (req, res) => {
Dates.find( query )
.then(Booked_Dates => res.json({ Booked_Dates }))
.catch(err => res.status(400).json({ errors: parseErrors(err.errors) }));
});


Also tried const query = { date_out : { $gte : 'ISODate("${today}")' }};



' = `



Can you please point me in the right direction.



PS: If I take out the query it returns the proper result.



Also this is the schema:



import mongoose from "mongoose";

const schema = new mongoose.Schema({
user: { type: String, required: true },
date_in: { type: String, required: true },
date_out: { type: String, required: true },
userId: { type: mongoose.Schema.Types.ObjectId, required: true }
});

export default mongoose.model("Date", schema);









share|improve this question
























  • how it worked for you.
    – Falcescu Alin
    Nov 11 at 23:30












  • I added your test data in mongo ... used a simple mongoose/nodejs app and queried the collection with your query. Got the expected result. Good luck.
    – Akrion
    Nov 11 at 23:32










  • Still not working for me. If I use the MONGO connector works as expected, when I use MONGOOSE it is not working...
    – Falcescu Alin
    Nov 11 at 23:51










  • The dates in MongoDB are NOT strings. They are BSON Date types when returned using ISODate() as a helper in the shell. So you're schema is casting values as "string" and then they do not match. Change the schema date_out: { type: Date } so it actually casts as a BSON Date.
    – Neil Lunn
    Nov 11 at 23:54






  • 1




    @Akrion Well you can always flag the comments on your answer for deletion. I can't see any logical reason to reject the flags since the "context" is the answer is changed after the comments were made and the comments no longer refer to the new content and can be removed. Or, leave the original content for context and include the "edit". There likely is a duplicate issue, but I can't find one right now, so an answer is still valid.
    – Neil Lunn
    Nov 12 at 0:04
















0














Why is mongo client returning an answer for the query and Mongoose is not.



db.dates.find({"date_out" : { $gte : ISODate("2018-11-12T00:00:00.000Z") }}).pretty()
{
"_id" : ObjectId("5be8a9eeda7bbc1fc40c1fa1"),
"user" : "some.user@example.com",
"date_in" : ISODate("2018-11-11T22:15:10.095Z"),
"date_out" : ISODate("2018-11-14T22:00:00Z"),
"userId" : "5be5a96e6db7be0568ea6e47"
}


But when I try to do the same query in Mongoose it doesn't return anything



import Dates from "../models/Date";
import moment from 'moment';
const today = moment().startOf('day').toDate(); // 2018-11-11T22:00:00.000Z
const query = { date_out : { $gte : today }};

router.get("/unavailable", (req, res) => {
Dates.find( query )
.then(Booked_Dates => res.json({ Booked_Dates }))
.catch(err => res.status(400).json({ errors: parseErrors(err.errors) }));
});


Also tried const query = { date_out : { $gte : 'ISODate("${today}")' }};



' = `



Can you please point me in the right direction.



PS: If I take out the query it returns the proper result.



Also this is the schema:



import mongoose from "mongoose";

const schema = new mongoose.Schema({
user: { type: String, required: true },
date_in: { type: String, required: true },
date_out: { type: String, required: true },
userId: { type: mongoose.Schema.Types.ObjectId, required: true }
});

export default mongoose.model("Date", schema);









share|improve this question
























  • how it worked for you.
    – Falcescu Alin
    Nov 11 at 23:30












  • I added your test data in mongo ... used a simple mongoose/nodejs app and queried the collection with your query. Got the expected result. Good luck.
    – Akrion
    Nov 11 at 23:32










  • Still not working for me. If I use the MONGO connector works as expected, when I use MONGOOSE it is not working...
    – Falcescu Alin
    Nov 11 at 23:51










  • The dates in MongoDB are NOT strings. They are BSON Date types when returned using ISODate() as a helper in the shell. So you're schema is casting values as "string" and then they do not match. Change the schema date_out: { type: Date } so it actually casts as a BSON Date.
    – Neil Lunn
    Nov 11 at 23:54






  • 1




    @Akrion Well you can always flag the comments on your answer for deletion. I can't see any logical reason to reject the flags since the "context" is the answer is changed after the comments were made and the comments no longer refer to the new content and can be removed. Or, leave the original content for context and include the "edit". There likely is a duplicate issue, but I can't find one right now, so an answer is still valid.
    – Neil Lunn
    Nov 12 at 0:04














0












0








0







Why is mongo client returning an answer for the query and Mongoose is not.



db.dates.find({"date_out" : { $gte : ISODate("2018-11-12T00:00:00.000Z") }}).pretty()
{
"_id" : ObjectId("5be8a9eeda7bbc1fc40c1fa1"),
"user" : "some.user@example.com",
"date_in" : ISODate("2018-11-11T22:15:10.095Z"),
"date_out" : ISODate("2018-11-14T22:00:00Z"),
"userId" : "5be5a96e6db7be0568ea6e47"
}


But when I try to do the same query in Mongoose it doesn't return anything



import Dates from "../models/Date";
import moment from 'moment';
const today = moment().startOf('day').toDate(); // 2018-11-11T22:00:00.000Z
const query = { date_out : { $gte : today }};

router.get("/unavailable", (req, res) => {
Dates.find( query )
.then(Booked_Dates => res.json({ Booked_Dates }))
.catch(err => res.status(400).json({ errors: parseErrors(err.errors) }));
});


Also tried const query = { date_out : { $gte : 'ISODate("${today}")' }};



' = `



Can you please point me in the right direction.



PS: If I take out the query it returns the proper result.



Also this is the schema:



import mongoose from "mongoose";

const schema = new mongoose.Schema({
user: { type: String, required: true },
date_in: { type: String, required: true },
date_out: { type: String, required: true },
userId: { type: mongoose.Schema.Types.ObjectId, required: true }
});

export default mongoose.model("Date", schema);









share|improve this question















Why is mongo client returning an answer for the query and Mongoose is not.



db.dates.find({"date_out" : { $gte : ISODate("2018-11-12T00:00:00.000Z") }}).pretty()
{
"_id" : ObjectId("5be8a9eeda7bbc1fc40c1fa1"),
"user" : "some.user@example.com",
"date_in" : ISODate("2018-11-11T22:15:10.095Z"),
"date_out" : ISODate("2018-11-14T22:00:00Z"),
"userId" : "5be5a96e6db7be0568ea6e47"
}


But when I try to do the same query in Mongoose it doesn't return anything



import Dates from "../models/Date";
import moment from 'moment';
const today = moment().startOf('day').toDate(); // 2018-11-11T22:00:00.000Z
const query = { date_out : { $gte : today }};

router.get("/unavailable", (req, res) => {
Dates.find( query )
.then(Booked_Dates => res.json({ Booked_Dates }))
.catch(err => res.status(400).json({ errors: parseErrors(err.errors) }));
});


Also tried const query = { date_out : { $gte : 'ISODate("${today}")' }};



' = `



Can you please point me in the right direction.



PS: If I take out the query it returns the proper result.



Also this is the schema:



import mongoose from "mongoose";

const schema = new mongoose.Schema({
user: { type: String, required: true },
date_in: { type: String, required: true },
date_out: { type: String, required: true },
userId: { type: mongoose.Schema.Types.ObjectId, required: true }
});

export default mongoose.model("Date", schema);






node.js mongodb date mongoose momentjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 23:21

























asked Nov 11 at 22:55









Falcescu Alin

11




11












  • how it worked for you.
    – Falcescu Alin
    Nov 11 at 23:30












  • I added your test data in mongo ... used a simple mongoose/nodejs app and queried the collection with your query. Got the expected result. Good luck.
    – Akrion
    Nov 11 at 23:32










  • Still not working for me. If I use the MONGO connector works as expected, when I use MONGOOSE it is not working...
    – Falcescu Alin
    Nov 11 at 23:51










  • The dates in MongoDB are NOT strings. They are BSON Date types when returned using ISODate() as a helper in the shell. So you're schema is casting values as "string" and then they do not match. Change the schema date_out: { type: Date } so it actually casts as a BSON Date.
    – Neil Lunn
    Nov 11 at 23:54






  • 1




    @Akrion Well you can always flag the comments on your answer for deletion. I can't see any logical reason to reject the flags since the "context" is the answer is changed after the comments were made and the comments no longer refer to the new content and can be removed. Or, leave the original content for context and include the "edit". There likely is a duplicate issue, but I can't find one right now, so an answer is still valid.
    – Neil Lunn
    Nov 12 at 0:04


















  • how it worked for you.
    – Falcescu Alin
    Nov 11 at 23:30












  • I added your test data in mongo ... used a simple mongoose/nodejs app and queried the collection with your query. Got the expected result. Good luck.
    – Akrion
    Nov 11 at 23:32










  • Still not working for me. If I use the MONGO connector works as expected, when I use MONGOOSE it is not working...
    – Falcescu Alin
    Nov 11 at 23:51










  • The dates in MongoDB are NOT strings. They are BSON Date types when returned using ISODate() as a helper in the shell. So you're schema is casting values as "string" and then they do not match. Change the schema date_out: { type: Date } so it actually casts as a BSON Date.
    – Neil Lunn
    Nov 11 at 23:54






  • 1




    @Akrion Well you can always flag the comments on your answer for deletion. I can't see any logical reason to reject the flags since the "context" is the answer is changed after the comments were made and the comments no longer refer to the new content and can be removed. Or, leave the original content for context and include the "edit". There likely is a duplicate issue, but I can't find one right now, so an answer is still valid.
    – Neil Lunn
    Nov 12 at 0:04
















how it worked for you.
– Falcescu Alin
Nov 11 at 23:30






how it worked for you.
– Falcescu Alin
Nov 11 at 23:30














I added your test data in mongo ... used a simple mongoose/nodejs app and queried the collection with your query. Got the expected result. Good luck.
– Akrion
Nov 11 at 23:32




I added your test data in mongo ... used a simple mongoose/nodejs app and queried the collection with your query. Got the expected result. Good luck.
– Akrion
Nov 11 at 23:32












Still not working for me. If I use the MONGO connector works as expected, when I use MONGOOSE it is not working...
– Falcescu Alin
Nov 11 at 23:51




Still not working for me. If I use the MONGO connector works as expected, when I use MONGOOSE it is not working...
– Falcescu Alin
Nov 11 at 23:51












The dates in MongoDB are NOT strings. They are BSON Date types when returned using ISODate() as a helper in the shell. So you're schema is casting values as "string" and then they do not match. Change the schema date_out: { type: Date } so it actually casts as a BSON Date.
– Neil Lunn
Nov 11 at 23:54




The dates in MongoDB are NOT strings. They are BSON Date types when returned using ISODate() as a helper in the shell. So you're schema is casting values as "string" and then they do not match. Change the schema date_out: { type: Date } so it actually casts as a BSON Date.
– Neil Lunn
Nov 11 at 23:54




1




1




@Akrion Well you can always flag the comments on your answer for deletion. I can't see any logical reason to reject the flags since the "context" is the answer is changed after the comments were made and the comments no longer refer to the new content and can be removed. Or, leave the original content for context and include the "edit". There likely is a duplicate issue, but I can't find one right now, so an answer is still valid.
– Neil Lunn
Nov 12 at 0:04




@Akrion Well you can always flag the comments on your answer for deletion. I can't see any logical reason to reject the flags since the "context" is the answer is changed after the comments were made and the comments no longer refer to the new content and can be removed. Or, leave the original content for context and include the "edit". There likely is a duplicate issue, but I can't find one right now, so an answer is still valid.
– Neil Lunn
Nov 12 at 0:04

















active

oldest

votes











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%2f53254059%2fmongoose-gte-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53254059%2fmongoose-gte-date%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

Full-time equivalent

Bicuculline

さくらももこ