Date comparison in mongodb - mongodb

I want to retrieve all the documents after a particular date.
My database has date as - DateAdded:"2014-12-17 10:03:46.000Z"
I wrote the following query-
db.collection.find({DateAdded:{"$lte":new Date("2015-06-17 10:03:46.000Z")}})
But the results doesn't fetches any record even though there are records for the dates upto 2015-06-24.

You can use ISODate to compare dates:
"$lte" : ISODate("2015-06-17T10:03:46Z")
ISODate works because that is the format your date is in.
new Date()
wraps the date in an ISODate helper, but is not able to convert in your query.
Check out this link for more information: https://www.mongodb.com/docs/v4.2/core/shell-types/

Use ISODate
format:
{'field': {$operator: ISODate(yyyy-MM-ddThh:mm:ss.msZ)}}
example:
db.getCollection('yourCollection').find({'sampleField': {$lte: ISODate('2015-06-17T10:03:46.000Z')}})

You can use this:
db.collection.find({DateAdded:{"$lte":new Date("2017-11-01")}})
It will convert your date format into this:
ISODate("2017-11-01T00:00:00Z")

Related

Format the inserted date in MongoDB

how can we format the date when you insert it into a collection in MongoDB ?
db.collection('transactions').insertOne({
amount: 1,
id: req.params.userid,
date: new Date ()
})
I tried things like
new Date (<YYYY-mm-dd>)
new Date ("<YYYY-mm-dd>")
But it doesn't work.
Google isn't really helping either.
Thank you
It is not possible with Date type
Reference
new Date() returns the current date as a Date object. mongosh wraps the Date object with the ISODate helper. The ISODate is in
UTC
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
On the other hand, you could do the below things
You can either use String type, but you need to convert to date before doing any queries
or
You need to convert to the desired format before returning to the client.

Retrieve an ISO type date, convert into DateTime and find oldest date from a set of docs

{
id:ObjectID(12fgrh567d8vb9567890578192)
.
.
.
meta:{
'date':ISO(1997-07-16T19:20:30.45+01:00)
}
}
I have this kind of about 500 documents, what I intend to do is define a function in using (pymongo) to retrieve the oldest date from all the documents. I am confused whether I should use the $min function in the ISO format or should I convert it to date time and then go for the $min.
Use following query:
db.collectionname.find({}).sort({'meta.date': 1}).limit(1);
this will first sort all the documents with the meta date. and will return the oldest one.
$min will directly work on IsoDate. No need for conversion.

Querying for farthest timestamp

How can I search my mongodb database for the document with the farthest timestamp? Timestamp being a date, ISODate format or not.
If you use ISODate format you can use $max in aggregate query to get oldest date.
Refer http://docs.mongodb.org/manual/reference/operator/aggregation/max/

How to convert date format in mongodb

I am using an API (so I have no control over the fields) - one of the fields is of type Date.
Now, I find that the date stored in the records resembles the following value:
{
...
"foobarDate": "2013-05-26T05:00:00.000Z",
...
}
Now I wish to retrieve some records based on this date - for example, I need to retrieve all the records whose fooBarDate is earlier than today's date.
If I use Javascript's Date() functionality, then I get the date in the following format:
Wed Jun 19 2013 21:13:50 GMT+0530 (IST)
If I try to design my query as follows...
{
"fooBarDate": {
"$lte": <the date calculated in javascript>
}
}
...I do not get the records - I get an empty array - I suspect it is due the reason that the format of the Date stored in MongoDB is different from the one I am passing.
How to convert the Javascript date format to the mongodb format? Or at least, how do I get the current date in the format stored in MongoDB?
P.S. I am using nodeJs and wish to query the DB in this case.
Mongo stores dates as ISODate objects. Just wrap our date into ISODate constructor:
{
"fooBarDate": {
"$lte": ISODate("2013-05-26T05:00:00.000Z")
}
}
The correct data format if you pass a string into the query is ISO8601 which is looks like you have done, however you can also pass the javascript date object into the query directly and get the same result. It looks like you may be missing the proper brackets which could be causing your query to fail. Try reformatting your code in this manner (edit I am using mongoose, but I think straight mongo is the same):
var currentTime = new Date;
orm.Records.find({lastUpdate:{$lte:currentTime}})

I want to query by date stamp which is not a ISODate format.The date is the following format Datestamp: 2016-03-08T02:20:20

I am new to Mongodb. I want to query by DateStamp. The date stamp is as follows
Datestamp: 2016-03-08T02:20:20
I used ISODate query which didn't work
db.GeneralLiability.find({ "session._Datestamp":{"$gte":ISODate("206-03-08T02:20:20")}})
Also I tried using:
db.GeneralLiability.find({ "session._Datestamp": {'$regex':'2016-03-08'}})
I am not sure if this is the right way to query. Please help me with this..
Show the output of the query.
You have written 206 instead of 2016.
db.GeneralLiability.find({ "session._Datestamp":{"$gte":ISODate("206-03-08T02:20:20")}})
Try this it should work:
db.GeneralLiability.find({ "session._Datestamp":{"$gte":ISODate("2016-03-08T02:20:20")}})