Mongodb filter out Date() and ISODate() from same field - mongodb

This is my schema for the date field
startDate: {type: Date, default: Date.now},
Somehow this field have 2 different value like this
Date(29906992800000) and ISODate("2022-05-31T03:23:32.193-06:30")
Now I am trying to filter out Date(...) to convert to ISODate(...). Is there any way to query such values? Conversion I can handle it through node application.
First
db.<collection>.find({_id:<id>}, {_id:0, <field>:1})
returns
{"startDate": Date("29906992800000") }
Second
db.<collection>.aggregate([{$match:{_id:<id>}, {$project:{_id:0, t:{$type:"<field>"}}}}])
returns
{"t": "string"}

Thank you #Alex I am posting as answer.
We have imported the data to MongoDB Compass, and used the normal sort method to filter out these dates and updated manually. Before I used Robo 3T may be that have some limitations.

Related

mongodb compass query with objectId in date range

I'm trying to perform a date range query on the _id field using compass.
I've tried what I found here with the following filter:
{_id: { $gte: ObjectId.fromDate(new Date('2019-01-01')) } }
What am I missing? I'd like to get a list of all documents from some date forward (in this example from 1 Jan 2019 to present). Unfortunately there isn't a timestamp in the document fields so I need to extract it from the object id.
You need to pass a date object to the ObjectId.fromDate, not a string. Try this:
ObjectId.fromDate(new Date('2019-01-01'))
This function works only in the shell and doesn't exist in the drivers.
EDIT after comments:
Here is a solution that works in Compass as well:
{
$expr: {
$gte: [
{"$toDate":"$_id"},
ISODate("2021-01-01T00:00:00.000Z")
]
}
}
Keep in mind, however, that it requires a version of mongo of 4.0+. You can checkout the docs here.
Also, checkout this related topic: Can I query MongoDB ObjectId by date?
It is not about Compass, but it provides solutions for generating the ObjectIds from a date without being dependent on ObjectId.fromDate().
I found a solution, maybe not the best but it does the job. Hopefully this can help someone with the same problem if there isn't a cleaner solution.
I converted the date I needed to an ObjectId outside of compass online here.
Then I wrote the query with that ObjectId:
{_id: { $gte: ObjectId(' object id here ') } }
As suggested in the comments, see this related topic. It's not specific to Compass but it provides a solution for generating ObjectIds from a date without being dependent on ObjectId.fromDate().

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

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.

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.

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

Mongodb get information by date (timestamp stored in db)

I need to get all information stored in my (mongo)DB in a collection filtered by a date.
The problem is that I store a timestamp in my BD, and I need to get all information for a day.
Example, if I chose the 2014-01-08, I have to get all the treatment done during this day
Can you help me with the mongo request ?
Thanks for advance
Query by using a date range with a "range based query", which is a simple treatment of the $gte and $lt operators:
Assuming you actually mean "YYYY-MM-DD" as in "year" "month" "day":
db.collection.find({
"dateField": {
"$gte": new Date("2014-08-01"),
"$lt": new Date("2014-08-02")
});
So that basically selects all items that fall within that date range being only occurring on "2014-08-01" only. So the range is all values that fall within that range.
If your timestamp is actually a number ( milliseconds since epoch ) then you just extract those values from date objects you create and send those in the query:
db.collection.find({
"dateField": {
"$gte": new Date("2014-08-01").valueOf(),
"$lt": new Date("2014-08-02").valueOf()
});
The same principle applies for just about any language as the supported DateTime object will have a method to do so.
If you are doing this then you might re-consider your approach. MongoDB uses a BSON Date type, which aside from the "type" identifier, actually stores the timestamp number internally. So there is basically no overhead to BSON dates, and they automatically inflate to DateTime objects with all drivers, as well as being supported by other internal operators.