I have a field startTime in MongoDB collection that stores time in the following form:
2015-07-22 08:19:04.652Z
I would like to find all documents that has the startTime greater than or equal to yesterday's time(from exactly one day before). I tried using the $currentDate in the find query, but I was not able to make it work.
EDITED:
Sample Document:
{
"_id" : ObjectId("55af5241e4b0ec7c53360333"),
"startTime" : ISODate("2015-08-22T08:19:04.652Z"),
"sampleId" : "SS10"
}
EDITED 2: No aggregation framework allowed.
Compute the previous date first the pass it in find query.
In javascript:
var date = new Date();
date.setDate(date.getDate() - 1);
db.col.find({'startTime':{'$gte':date}})
Related
I have collection Ticket_master,which like as follows
Ticket_master
{
_id:5e70ed53de0f7507d4da8dc2,
"TICKET_NO" : 1000,
"SUBJECT" : "Sub1",
"DESCRIPTION" : "Desc1",
"createdAt" : ISODate("2020-03-17T15:31:31.039Z"),
}
Already tried with the following code snippet and it returns null result set.
db.getCollection('ticket_masters').find({ 'createdAt':ISODate('2020-03-17T00:00:00.000Z')})
How fetch fetch data from ticket_master collection by matching the value with date of creation column,which is represented as createdAt.
Following code snippets return the results. Because it contain both date and time.How fetch data from the collection only with Date .
db.getCollection('ticket_masters').find({ 'createdAt':ISODate('2020-03-17T15:31:31.039+00:00')})
as long as you are searching for all the tickets that have been created on some day, so you can use a date range to filter the tickets
db.getCollection('ticket_masters').find({
createdAt : {
$gte: ISODate('2020-03-17T00:00:00.000Z'),
$lt: ISODate('2020-03-18T00:00:00.000Z')
}
})
this should return all the tickets that have been created on 17/03/2020
check this Mongo Playground
I have a use case to query as below.
The time stamp passed would like 2018-09-01T12:23:32 like ISODate. Mongo version 3.5. How to query in such cases?
mongo_client.find(
"date":{
'$gte': {<midnight on the date of date key},
'$lt': {<searchkey with time stamp>}
})
you can create start and end dates, use it for querying
> var end = new Date("2018-01-30T04:05:23.974Z")
ISODate("2018-01-30T04:05:23.974Z")
> var start = new Date("2018-01-30")
ISODate("2018-01-30T00:00:00Z")
> db.col.find({date : {$gte : start}, date : {$lt : end}})
I have around 30-40 records like the example before in my database and I'm looking to get the notifications that are less than 1 month old (from today's date). Is there a way in Mongo to get these results without having to pass in a today's date via JavaScript? Or if I do have to pass it in via JavaScript, how would I process this against my created date?
{
"_id" : ObjectId("48445b4dc72153e9ad7f3bfb"),
"notificationID" : "78723asd5-vnbb-xv31-afe0-fa9asf164e4",
"notification" : "Notification #1",
"created" : ISODate("2016-11-21T20:33:53.695Z")
}
Any help is appreciated, thanks.
MongoDB has its own Javascript interpreter so, unless your MongoDB server has a different date than your system, it knows the current date so you easily use simple Javascript to compute the value you're looking for using a regular Date object and use it in your query.
var d = new Date();
d.setMonth(d.getMonth() - 1); //1 month ago
db.data.find({created:{$gte:d}}); //change "data" for your collection's name
If you need a different date than your database's, I'm afraid you'll have to somehow pass it as a parameter.
const now = new Date()
const temp = new Date(now).setMonth(now.getMonth() - 6);
const priorSix = new Date(temp)
Table.find({"date" : {$gte: priorSix, $lt: new Date()}}, (err, tables) => {
if(err) throw new Error(err)
res.status(200).json(tables)
}).populate('foodList.item')
This code worked for me. It retrieves documents of the last 6 month :)
MongoDB document sample is given below:
{
"_id" : ObjectId("5f2df113bdde22f1043g45gg"),
"cd" : 1395376406,
"dob" : 552026006,
"e" : "test#gmail.com",
"g" : "M"
}
I need query to get birthday list for today using dob(date of birth) field(timestamp).
Thanks in advance!
I am not sure how elegant my solution is, but it should work
var start = new Date();
start.setHours(0,0,0,0);
var end = new Date();
end.setHours(23,59,59,999);
db.collection.find({dob:{$gt:start.getTime(), $lt:end.getTime()}})
Since MongoDb doesn't have any inbuilt functions/operations to support parsing raw timestamps and extracting information from them, you need to do the operation by passing a custom java script function to the server and get it executed there. This function would be executed for each record though.
db.collection.find({$where:function(){
var recDate = new Date(this.dob);
var recDay = recDate.getDate();
var recMonth = recDate.getMonth();
var today = new Date();
var todayDate = today.getDate();
var todayMonth = today.getMonth();
return (recDay == todayDate && recMonth == todayMonth);
}});
The function simply checks if any record's day and month match today's day and month.
Although this works for timestamps, you should take advantage of MongoDBs ISODate data type whenever you store date information. This enables us to use various operators and functions on these fields. Moreover they would be faster. If your document had the below structure:
{
"_id" : "1",
"cd" : 1395376406,
"dob" : ISODate("2014-11-19T08:00:00Z"),
"e" : "test#gmail.com",
"g" : "M"
}
Where, the dob field is of ISODate type. You could easily do a aggregate operation to fetch the results.
var todayDate = ISODate().getDate();
var todayMonth = ISODate().getMonth();
db.collection.aggregate([
{$project:{"day":{$dayOfMonth:"$dob"},
"month":{$month:"$dob"},
"_id":1,"cd":1,"dob":1,"e":1,"g":1}},
{$match:{"day":todayDate,"month":todayMonth}},
{$project:{"cd":1,"dob":1,"e":1,"g":1,"_id":1}}
])
The above operation utilizes the functions and operations that are allowed on a ISODate field. Querying becomes a lot easier and better.
I have an issue with a MongoDB query that I am having challenges with. In my class, I have a structure of [instance].events.destination.estimatedDateTime, and the value of estimatedDateTime is an ISO Date (eg. 2014-09-23 21:48:00.000Z). We are using org.springframework.data.mongodb.core.query.Criteria to append criteria to our query like this:
criteria = criteria.and("events.destination.estimatedDateTime").gte(today)
When this is generated, it looks like this when evaluated at runtime:
"events.destination.estimatedDateTime" : { "$gte" : { "$date" : "2014-10-28T05:00:00.000Z"} }
Unfortunately that is returning no records. I have taken that same condition and attempted to pull records in RoboMongo, but again there are no results. However, I have found that when I do the following in RoboMongo, I do indeed get results
"events.destination.estimatedDateTime" : { $gte : ISODate("2014-10-28T05:00:00.000Z") }
Is there a way using the Criteria class to generate ISODate(date) rather than "$date":date?