ISODate is not defined - node.js

I am trying to get results from mongodb using nodejs/mongoose.
var dateStr = new Date(year,month,day,0,0,0);
var nextDate = new Date(year,month,day,23,59,59);
GPSData.find({"createdAt" : { $gte : new ISODate(dateStr), $lte: new ISODate(nextDate) }}, function(err, data) {
if(err)
console.log(err);
});
Error: ISODate is not defined

Note that ISODate is a part of MongoDB and is not available in your case. You should be using Date instead and the MongoDB drivers(e.g. the Mongoose ORM that you are currently using) will take care of the type conversion between Date and ISODate behind the scene.

In my case, I was converting a query with ISODates
let dateString = "2014-01-22T14:56:59.301Z";
$gte : ISODate( dateString )
in node.js is
$gte : new Date( dateString )

Convert date to MongoDB ISODate format in JavaScript using Moment JS
MongoDB uses ISODate as their primary date type. If you want to insert a date object into a MongoDB collection, you can use the Date() shell method.
You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date() constructor or the ISODate() function. These functions accept the following formats:
new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.
new Date("<YYYY-mm-ddTHH:MM:ss>") specifies the datetime in the client’s local timezone and returns the ISODate with the specified datetime in UTC.
new Date("<YYYY-mm-ddTHH:MM:ssZ>") specifies the datetime in UTC and returns the ISODate with the specified datetime in UTC.
new Date() specifies the datetime as milliseconds since the Unix epoch (Jan 1, 1970), and returns the resulting ISODate instance.
If you are writing code in JavaScript and if you want to pass a JavaScript date object and use it with MongoDB client, the first thing you do is convert JavaScript date to MongoDB date format (ISODate). Here’s how you do it.
var today = moment(new Date()).format('YYYY-MM-DD[T00:00:00.000Z]');
console.log("Next day -- " + (reqDate.getDate() + 1))
var d = new Date();
d.setDate(reqDate.getDate() + 1);
var tomorrow = moment(d).format('YYYY-MM-DD[T00:00:00.000Z]');
You can pass today and tomorrow object to MongoDB queries with new Date() shell method.
MongoClient.connect(con, function (err, db) {
if (err) throw err
db.collection('orders').find({ "order_id": store_id, "orderDate": {
"$gte": new Date(today), "$lt": new Date(tomorrow)}
}).toArray(function (err, result) {
console.log(result);
if (err) throw err
res.send(result);
})
})

Instead of ISO use "new Date" node js will take care of ISO itself, no need to write ISO just simply use "new Date"

You can simply use as follow to convert dates in ISO string :
GPSData.find({"createdAt" : { $gte : new Date(year,month,day,0,0,0).toISOString(), $lte: new Date(year,month,day,23,59,59).toISOString() }}, function(err, data) {
if(err)
console.log(err);
});

if (req.params.sDate && req.params.eDate) {
query["createdAt"] = {
$gte: new Date("2020-01-25").toISOString(),
$lte: new Date("2020-09-25").toISOString()
};
}
console.log("query", query, req.params.limit, req.params.skip);
domain.Payment.find(query)
.limit(req.params.limit)
.skip(req.params.skip)
.sort({ createdAt: -1 })
.exec((err, list) => {
console.log("err", err);
if (err || !list) {
callback(err, null);
} else {

Related

Working with dates: value provided is not ISO format - Node.js and moment.js

I am using Moment.js to transform string from req.query to new Date() object and use it in mongo.db aggregation $gte and $lt (classic search by date range). In the base I store date as Date (format yyyy-mm-dd -> example: manufacturingDate: 2022-06-06T07:00:00.000+00:00)
The problem is, that I am getting in my console and error: Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged.
My api - backend code is:
The isValid() function I did because, sometimes I choose only startDate and then it gives me Invalid date for endDate
router.get("/searchDate", async (req, res) => {
try {
function isValid(data: any) {
if (data == "Invalid Date") {
return new Date(moment("2100-11-11"));
} else {
return data;
}
}
const { startDate, endDate } = req.query;
const containers = await Container.aggregate([
{
$match: {
$or: [
{
manufacturingDate: {
$gte: isValid(new Date(moment(startDate))),
$lt: isValid(new Date(moment(endDate))),
},
},
],
},
},
]);
res.status(200).json(containers);
} catch (err) {
res.status(404).json({ success: false, msg: "Container not found" });
}
});
In the req.query I pass the date in format (yyyy-mm-dd), example: 2022-06-06, BUT if I chose only FROM, then this problem occurs because I send an endDate = undefined to my API.
Code is:
onChangeDate() {
function convert(str: any) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
this.containersService
.getContainersByDateRange(convert(this.from), convert(this.to))
.subscribe((containers: any) => {
this.containers = containers;
});
}
I know that I can't convert undefined and thats why I than get this error. Is there some workaround trick?

How to fetch data when post the date is string in node js

In my application i want to fetch the record on the bases of date i am posting the date like this (2019-07-27) and in my mongodb the date stores like ("created_at" : ISODate("2019-07-27T16:01:24.636+05:00")) but i return empty object how can i solve this problems any body help thanks in advance.
this is my mongodb data
http://prntscr.com/opi9lm
this is my post request
{
"income_frequency" : "daily",
"from_date" : "2019-07-27"
}
this is my controller
const transactionsCredit = await driverTransactionModel.find( {
$and: [
{ user_id: req.userData.userId },
{ transaction_type: mainConfig.transactionType.moneyIn },
{ created_at: req.body.from_date }
]
} ).lean().exec();
var from_date = new Date('2019-07-27'.toISOString());
db.collection.findOne('from_date': {
$lte: from_date
}, function(err, doc) {
if (error) {
console.log(error);
} else {
console.log(doc);
}
});
Note: You can convert date to ISODate and then find documents from collection will resolve your issue.
You need to make 2 dates start date and end date and then match also in DB there's "created_at" which is in date format while date you received from the front end is in a string that's why you need to cast it to date first before matching in the query as
let startDate = new Date(req.body.from_date)
// this will give you date with utc time 00:00:00
let endDate = new Date(new Date(req.body.from_date).setUTCHours(23,59,59))
then match with start and end date in the query:
If you only want data of date of req.body.from_date
const transactionsCredit = await driverTransactionModel.find( {
user_id: req.userData.userId,
transaction_type: mainConfig.transactionType.moneyIn,
created_at:{$gte:startDate,$lte:endDate}).lean().exec();
If you want to fetch data greater than date(req.body.from_date)
const transactionsCredit = await driverTransactionModel.find( {
user_id: req.userData.userId,
transaction_type: mainConfig.transactionType.moneyIn,
created_at:{$gte:startDate}).lean().exec();

Unable to query data between two dates in MongoDB

db.test.find({"date":{$gte:"2017-04-11",$lt:"2017-04-13"}},function(err,doc){
console.log("date function called");
res.json(doc);
console.log(doc);
});
code works fine in mongodb and output,but in nodejs the output is empty array.
Collections can be queried with find. The result for the query is actually a cursor object. This can be used directly or converted to an array. Making queries with find()
cursor.toArray(function(err, docs){}) converts the cursor object into an array of all the matching records. Probably the most convenient way to retrieve results but be careful with large datasets as every record is loaded into memory. toArray
The mongo shell wraps objects of Date type with the ISODate helper; however, the objects remain of type Date. Return Date
var collection = db.collection('test');
collection.find({ "date": { $gte: new Date("2017-04-11"), $lt: new Date("2017-04-13") } })
.toArray(function (err, doc) {
console.log("date function called");
if (err) {
console.error(err);
} else {
console.log(doc);
res.json(doc);
}
});

Purging Data Logic in MongoDb based on Dates

I am unable to think of logic for the following Issue.
Consider I want to Store Data in mongoose in collection called packet where I can store data of different users. I want to purge the data once a certain threshold has been reached (Say for example 10 days). We know that the Mongoose by default gives us CreatedAt and UpdatedAt fields.
Suppose my data is created at 22nd February 2015 and Current Date is 24th February 2015.I will have a PurgeData number(column used for purging of data) as 2 (difference between the two dates). Every day I want to change the value of the PurgeData number by comparing the difference between the current date and the CreatedAt date. I want to schedule this operation every day and delete the data that has reached the threshold so I save memory space. Can Somebody help me with the logic for it and scheduling of the event?
Thanks in Advance
Haven't actually tried this but I would suggest some logic like schedule a job using the mongo-scheduler package where you first do an update and then remove the documents that breach the PurgeData condition. Something like this (untested):
/*
Scheduler Arguments
connection - mongodb connections string (i.e.: "mongodb://localhost:27017/scheduler-db") or a mongoose connection object
options - Options object
*/
var mongoose = require('mongoose');
var Scheduler = require('mongo-scheduer');
var scheduler = new Scheduler(connection, options);
var packet = new mongoose.Schema({
CreatedAt : {type : Date, default: Date.now},
PurgeData : {type : Number, index : true}
});
var Packet = mongoose.model('Packet', packet);
var moment = require('moment');
//Schedule the event.
var event = {
name: 'purge',
collection: 'packet',
after: new Date(),
cron: "0 15 10 * * ?" //cron string representing a frequency - fire at 10:15am every day
};
scheduler.schedule(event)
scheduler.on('purge', function(packet, event) {
console.log(packet.PurgeData)
Packet.find({"PurgeData": {"$lt": 10} }, function (err, packets) {
packets.forEach(function (p){
var days_diff = moment().diff(moment(p.CreatedAt), 'days') // using momentjs library
Packet.update(
{
"_id": p._id
},
{
"$set": {
PurgeData: days_diff
}
}, function (err, doc){
console.log(doc);
});
});
});
Packet.find({"PurgeData": {"$gte": 10} }, function (err, packets) {
packets.forEach(function (p){
Packet.remove({ "_id": p._id }, function (err){
handleErr(err);
});
});
});
})

how to get number of documents which is created today with mongoose?

how to get number of documents which is created today with mongoose? I'm using MEAN stack. I read Mongoose Api Docs but doesn't understand anything :/
By default there is no way.
Documents don't have creation date associated with them unless you explicitly store it in MongoDB.
In other words your Mongoose schema should have something like created field:
const blogSchema = new Schema({
created: {type: Date, default: Date.now}
});
Then search for matching docs:
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
doc.find({created: {$gte: today}}).exec(callback);
Supposing you have the date field createdAt in your schema and you would like to get the number of users registered today, you should create a two date object instances to use in your date range query, start and end dates.
Your start date object should hold the current date time hours at 00:00:00.000 (milliseconds precision) and set the hours for today's date to 23:59:59.999 to the end date variable:
var start = new Date();
start.setHours(0,0,0,0);
var end = new Date();
end.setHours(23,59,59,999);
Then pass the modified date objects as usual in your MongoDB aggregation pipeline in the $match operator:
var pipeline = [
{
"$match": {
"createdAt": { "$gte": start, "$lt": end }
}
},
{
"$group": {
"_id": null,
"count": { "$sum": 1 }
}
}
];
Model.aggregate(pipeline, function (err, result){
if (err) throw new Error();
console.log(JSON.stringify(result));
});
If you are using the momentjs library, this can be done by using the startOf() and endOf() methods on the moment's current date object, passing the string 'day' as arguments:
var start = moment().startOf('day'); // set to 12:00 am today
var end = moment().endOf('day'); // set to 23:59 pm today

Resources