How to embed the date-to-timestamp conversion in a mongodb query? - mongodb

Suppose I have documents in a mongo collections with a timestamp field. I want to query the documents based on the timestamp value (greater than, less than). In order to do that, instead of calculating each time the timestamp for my current date, I would like to embed the conversion from the date to timestamp in the query. I tried something like this (suppose "ts" is my timestamp field):
db.my_collection.find({"ts": {$lt: {$toLong : new Date("2020-12-09")}}})
instead of doing:
db.my_collection.find({"ts": {$lt: 1578524400}})
but it seems to return an empty result.
Is there a result to perform the date-to-timestamp conversion inside the query?

You can use $toLong but it's an aggregation pipeline operator, you need an aggregation pipeline not a plain find.

Related

Query mongodb for a specific date range

I've googled and googled this, and for the life of me I can't make it work...which I know I am just missing something totally simple, so I'm hoping someone here can save my sanity.
I have am trying to lookup a range of documents from a mongodb collection (using mongoose) based on a date range.
I have this code:
var startDate = new Date(req.query.startDate);
var stopDate = new Date(req.query.stopDate);
studentProgression.find({ $and: [ {'ACT_START': {$gte: startDate, $lte: stopDate} } ]})
But it doesn't return any documents.
The date in the MongoDb collection is stored as a string, and looks like this (for example):
ACT_START: "25-MAY-20"
a
I know I'm probaby tripping somewhere with the fact it's a string and not a date object in mongo, as I haven't had this issue before. I'd rather not go through the collection and change all the string dates to actual date objects.
Is there anyway to do this lookup the way I've laid it out?
Unfortunately, you have to change the format of your ACT_START field and cast it to a Date.
By doing $gte and/or $lte on a string field, mongo will compare the two strings based on their alphabetical order.

why does $eq comparison is not working on mongodb with dates

I have the following query on mongo
db.getCollection('someCollection').find({"status":"failed","start_date": {$gt: new Date("2019/03/01")}})
that will retrieve all the records that have a failed status and the start_date is equal or greater than "2019/03/01".
But when i try to only retrieve records specifically for "2019/03/01":
db.getCollection('someCollection').find({"status":"failed","start_date": {$eq: new Date("2019/03/01")}})
It doesn't retrieve anything.
$lt and $gt queries work is just $eq that doesn't work. is that the correct way to use the $eq?
thank you
When you use new Date("2019/03/01") the actual date being searched is 2019/03/01 00:00:00.00. That is, the Date is exactly midnight 2019/03/01 (down to the millisecond). Unless your record also has the exact same date recorded, it will not match. That's why you need to use {$gt: new Date("2019/03/01"), $lt: new Date("2019/03/02")}. Just think of it as that 1 "day" is actually a range of 24 hours. Which is why you need to specify it as a range, and not as a simple $eq comparison.
The reason why your first query works is because when you search for $gt 2019/03/01 you're really searching for $gt 2019/03/01 00:00:00.00. And so, of course every record started on 2019/03/01 will match.
One thing to take note of: technically, if you have a document made exactly at midnight i.e. with a timestamp of 2019/03/01 00:00:00.00, it won't match. So you really should use {$gte: new Date ('2019/03/01')} (note the extra 'e'). $gte is greater than or equal. This may sound trivial but is actually important, because if you don't use actual timestamps and record just the date when you create the record (i.e. you insert with {start_date: new Date('2019/03/01')}, all those timestamps will actually have 00:00:00.00 as their time component, and they won't match with a $gt comparison, only a $gte comparison. You probably use new Date() when creating the record, so you're getting the full timestamp, which is why it hasn't bitten you yet. But to be semantically correct, you should use $gte in your first query. Does that make sense?

ISO8601 Date Strings in MongoDB (Indexing and Querying)

I've got a collection of documents in MongoDB that all have a "date" field, which is an ISO8601 formatted date string (i.e. created by the moment.js format() method).
I'd like to be able to efficiently run queries against this collection that expresses the sentiment "documents that have a date that is after A and before B" (i.e. documents within a range of dates).
Should I be storing my dates as something other than ISO-8601 strings? Contingent upon that answer, what would the MongoDB query operation look like for my aforementioned requirement? I'm envisioning something like:
{$and: [
{date: {$gt: "2017-05-02T03:15:22-04:00"}},
{date: {$lt: "2017-06-02T03:15:22-04:00"}},
]}
Does that "just work"? I need some convincing.
You definitely want to use the built-in date data type with an index on your date field and not strings. String comparison is going to be slow as hell compared to comparing dates which are just 64bit integers.
As far as querying is concerned, check out the answer here:
return query based on date

View last N documents using MongoDB Compass

I wish to view in MongoDB Compass the last N documents in a very large collection; too many to scroll through.
I could .skip(total - N) if I knew the syntax for that within Compass.
Alternatively, I have a date field and could use $gte with a date if I knew how to express a date in a manner acceptable to Compass.
Suggestion/example how to do this, please?
MongoDB Compass 1.6.1(Stable)
For date comparison you need to use $date operator with a string that represents a date in ISO-8601 date format.
{"date": {"$gte": {"$date": "2017-03-13T09:51:26.317Z"}}}
In my case the values of date field in Compass and mongo shell are different. So firstly I query the documents in the shell and then copy the "2017-03-13T09:51:26.317Z" from the result to the Compass filter line. In mongo shell it look like:
{
...
"date" : ISODate("2017-03-13T09:51:26.317Z"),
...
}
MongoDB Compass 1.7.0-beta.0 (Beta)
This version have an advanced query bar that lets you input not just the filter (as before), but also project, sort, skip and limit
(#Oleksandr I learned from your effective answer; thank you.)
I've also been shown that the Compass Schema tab allows one to drag a date range on the _id field to apply a filter query for that range. That range can be successively narrowed as desired.
Skip is descibed here
https://docs.mongodb.com/compass/current/query/skip/
In the Query Bar, click Options.
Enter an integer representing the number of documents to skip into the Skip field
Click Find to run the query and view the updated results.

Filtering & sorting by dates in MongoDB's oplog

I'm trying to filter oplog.rs to view operations that were logged after a certain date. But comparison operators don't seem to work with dates in MongoDB:
db['oplog.rs'].find({ts: {$gte: ISODate("2014-12-15T00:00:00Z")}})
What's confusing is a lot of online sources are saying to do it this way but it is obviously not working. I want to return results where the ts field is at least December 15th or more recent, but I am getting results that are clearly before this period. If I switch $gte with $lte no results at all are displayed, even though there are definitely entries both before and after this specified date.
Also, I can't seem to sort the results either. If I try this:
db['oplog.rs'].find().sort({ts: -1})
I get this:
error: {
"$err" : "Runner error: Overflow sort stage buffered data usage of 33566005 bytes exceeds internal limit of 33554432 bytes",
"code" : 17144
}
If I could filter the results to make them more recent, I am hoping it would overcome this sorting error, but I can't even do that with MongoDB's basic operators. How can I filter the results of a find operation by date?
Comparion operators work just fine with date, but ts is a Timestamp object - not a date.
Your query needs to look something like this:
db['oplog.rs'].find({ts: {$gte: Timestamp(ISODate("2014-12-15T00:00:00Z").getTime(),0)}})
This creates a Timestamp object based on an ISODate and uses it in the query.