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);
}
});
The $gte operator is one of the most commonly used operators in a query. This operator returns the documents where the value of the specified field is greater than or equal to the given value. In this article, we will discuss how to use the mongoose $gte operator.
MongoDB provides different types of comparison operators and greater than equals to operator ($gte) is one of them. This operator is used to select those documents where the value of the field is greater than equals to (>=) the given value. You can use this operator in methods (like, find (), update (), etc.) according to your requirements.
After installing the mongoose module, you can check your mongoose version in command prompt using the command. After that, you can just create a folder and add a file for example, index.js as shown below. Database: The sample database used here is shown below:
It returns all the documents where the value of the age field is greater than or equal to 25. The $gt operator stands for “greater than” operator. It works in the same way as the $gte does, but with a single exception. It only returns the documents where the value of the specified field is greater than the given value.
It should 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 documents
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With