Maria Jane Maria Jane - 1 month ago 16
Node.js Question

$gte and $lte in mongoose with multiple condition

Am I using

$gte
and
$lte
wrongly with other condition? I got empty array, but when I do just
user_email:req.bodu.user_email
I'm able to get all the data.

function firstDayOfMonth() {
var d = new Date(Date.apply(null, arguments));
d.setDate(1);
return d.toISOString();
}

function tomorrowDate() {
var d = new Date();
d.setDate(d.getDate() + 1)
return d.toISOString();
}

Users.find({
"user_email": req.body.user_email,
"createdAt": {
"$gte": firstDayOfMonth(),
"$lte": tomorrowDate()
}
},
function(err, response) {
if (!err) {
console.log(response)
res.json(response);
}
});

Answer

It should be work

I just tested with these docs

/* 1 */
{
    "_id" : ObjectId("57f9cc3dc4ac279a7c539d0f"),
    "TechActivityId" : 0,
    "LicenseId" : 0,
    "created" : "2016-01-01T00:00:00.000Z",
    "StateofActivity" : "State of Activity",
    "TechGId" : "123121134"
}

/* 2 */
{
    "_id" : ObjectId("57f9e674c4ac279a7c539d10"),
    "TechActivityId" : 0,
    "LicenseId" : 0,
    "created" : "2016-10-10T06:28:37.146Z",
    "StateofActivity" : "State of Activity",
    "TechGId" : "1231234"
}

db.getCollection('hola').find({"created":{"$gte":"2016-01-01T00:00:00.000Z","$lte":"2016-10-10T06:28:37.146Z"}})

And I am getting both docs

Comments