mongodb compass query with objectId in date range - mongodb

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().

Related

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

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.

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.

Can you update a collection in MongoDB and remove the first/last char on one field?

My question might be simple be here's some more context to it.
I have a MySQL DB, I've used an ETL tool to populate a MongoDBwith, however I couldn't manage to create proper ObjectId reference to it (I can only get a string of the ObjectId.
So far I've had an idea (maybe crazy but still.. could work)
I got this field populated like this in one document :
"field1" : "ObjectId('5d48845c456145ee9d1ccffde')",
What I would want to achieve through mongoDB is removing the first and last char to get (stripping the double quotes):
"field1" : ObjectId('5d48845c456145ee9d1ccffde'),
(note that MongoDB seems to automatically convert simple to Double quote after the change, so my reference become corret).
Problem is, I don't find anything close to a sort of Update script for MongoDB to achieve this.
Is there any way to do this ?
Using NodeJS could work, however, querying the document at this state doesn't return the field1 (probably cause it find it incorect)...
If its one time update, you can use the following query:
db.COLLECTION.aggregate([
{
$addFields:{
"field1":{
$toObjectId:{
$substrBytes:[
"$field1",
10,
24
]
}
}
}
},
{
$out:"COLLECTION"
}
])
In aggregation, the 'field1' is cast to ObjectId. Later on, the old data in the collection is replaced with the aggregated one.

MongoDb Aggregation toDate() in Spring Data Mongo

I am trying to recreate an aggregation pipeline I built in mongo compass in Java. It consists of an $addFields which converts string to date using $toDate. I am trying to do the same in Java with Spring Data Mongo Aggregation Pipeline but have failed thus far and need help
I tried with some other example with date from string and projection but I don't know how to work it with $addFields.
This is the mongo pipeline that I need to be recreated in Java with Spring Data.
{
"event.currentTimeStamp": {
"$toDate": "$event.timestamp"
}
}
This works fine from shell and Compass.
This is my sample Document .
{
"_id":"5c0f0f97ece8cc0009c0a8f8",
"event":{
"jobid":"e4955ab0-003a-40fd-ac5a-4363c3e0f604",
"username":"sn",
"timestamp":"Mon Dec 10 20:15:02 EST 2018",
"system":"Mercury",
"eventStatus":"START",
"eventType":"UPLOAD"
}
I need the timestamp field to be converted to date and added as a field so that I can do other aggregation operations on them.
NOTE: Hoping to get an answer as a form of aggregation as that's what I am doing. Thanks.
Please find the below answer. Hope this will help you.
db.getCollection('test1').aggregate([
{$addFields : {"date" : {"$toDate" : "$event.timestamp"}}}
])

MongoDB - get documents between dates using ObjectID

As the title said, I want to get documents between 2 dates using the ObjectID. In mongoose I use _id: { $gte: ObjectID.createFromTime(new Date("2019-01-01")) } but I can't figure it out how to construct this query on mongo shell. There is a post on StackOverflow which lists different queries on mongo shell and mongoose but I can't seem to find that post. Does anyone know how to do the same query on mongo shell? Thanks.
UPDATED
Since I have already found the solution, does anyone know why the method names are different in mongo shell and mongoose? This really causes confusion sometimes.
Figured it out, it's _id: { $gte: new ObjectId.fromDate(new Date("2019-01-01")) } on mongo shell