Meteorjs Mongodb how to query a date with momentjs - mongodb

In Meteor, I create a winner document this way:
var winner = {
participant_id: array[randomIndex]["_id"], //don't worry about the array[randomIndex]
creation_date: new Date()
};
id = Winners.insert(winner);
Later, I want to know how many winners I have today. I tried it many ways, but I couldn't succeed to get the right result.
The last thing I tried is this one:
Winners.find({creation_date: {"$gte": moment().startOf('day'), "$lt": moment().add('days',1)}}).count();
But the result is always equal to zero.
I guess the reason is that moment().startOf('day') is not a date object but I've no clue how to query it the right way.

You need to convert the moment object back to a Date so meteor/mongo can process the query. You can do that by appending a call to toDate like so:
var startDate = moment().startOf('day').toDate();
var endDate = moment().add('days',1).toDate();
Winners.find({creation_date: {$gte: startDate, $lt: endDate}}).count();

Try this:
// the number of milliseconds since 1970/01/01:
creation_date:new Date().getTime()
and
// you can still use moment:
moment().startOf('day').valueOf()
// query:
Winners.find({
creation_date: {
"$gte": moment().startOf('day').valueOf(),
"$lt": moment().add('days',1).valueOf()
}
}).count();

Related

Query to trim the data from existing mongodb database field

I have a field DateOfBirth of type String. Currently i am storing the date in the format yyyy-MM-ddTHH:mm:ss Z.
Now i want to change the date format in the existing database as yyyy-MM-dd i don't want time now i just want to trim that how can i do that using a query in mongodb.
db.profile.update( {}, { $set : { "physical.dateOfBirth" : "2012-01-11" } }, true, true);
I tried using this query i did not get desired result.
Currently you cannot update values based on a current value in mongodd with the exception of increment and decrement.
You will first need to find each value and then update them before saving them again.
You could do it multiple ways:
get all profiles (only need _id and properties to update)
for each profile
set the value to its yyyy-MM-dd value
save profile
or
get all profiles (only need _id and properties to update)
create bulkOp
for each profile
set the value to its yyyy-MM-dd value
add update to bulkOp
execute bulkOp
or
aggregate to find distinct yyyy-MM-dd values
for each <yyyy-MM-dd value>
var match = {"physical.dateOfBirth": {$regex: <yyyy-MM-dd value>}};
var update = {$set: {"physical.dateOfBirth": <yyyy-MM-dd value>}};
var multi = {multi: true};
db.profiles.update(match, update, multi);
Clearly this is just the processes you could try and not actual code.
The last way would be the easiest on memory because you would not actually load much into memory, just the distinct values incase you have 100s or 1000s of values with the same dates.
Good luck
EDIT: something along the lines of this:
The $cond in the _id is just to match everything together in 1 item
var aggregate = db.profiles.aggregate({$group: {
_id: {$cond: ['_id', 'a', 'a']},
values: {$addToSet: { $dateToString: { format: '%Y-%m-%d', date: '$physical.dateOfBirth' } }}
}});
var values = aggregate[0].values;
for(var i = 0; i < values.length; i++) {
var date = new Date(values[i]);
var nextDay = new Date().setDate(date.getDate() + 1);
var match = {'physical.dateOfBirth': {$gte: date, $lte: nextDay}};
var update = {$set: {'physical.dateOfBirth': date}};
var multi = {multi: true};
db.profiles.update(match, update, multi);
}

The most efficient way to returning today's documents from a collection

I have a collection where all the documents in that collection have a datetime timestamp that includes the time as well as the date.
When using the aggregation framework how do I say give me the values for today? The following only gives me the entries for midnight.
db.strategy.aggregate(
[
{
$match: {
"date": new Date()
}
},
]
);
To get the documents which have datetime values for today, you need to create a date query range where the start date object should hold the current date time hours at 00:00:00.000 (milliseconds precision) and set the hours for today's date to 23:59:59.999 to the end date variable.
For example:
// Create the start date which holds the beginning of today
var todayStart = new Date();
todayStart.setHours(0,0,0,0);
// Create the end date which holds the end of today
var todayEnd = new Date();
todayEnd.setHours(23,59,59,999);
// Create the date range query object
var query = {
"date": {
$gte: todayStart,
$lte: todayEnd
}
};
// using the find() method
db.strategy.find(query);
Or using the aggregate() method as
db.strategy.aggregate({ "$match": query });
You should get the value in range I.e. from today's 12AM time stamp to today's 23:59:59 timestamp.
Var d = new Date();
var startDate = d.setHours(0,0,0);
var endDate = d.setHours(23,59, 59);
db.strategy.aggregate(
[
{
$match: {
"date": {$gte: startDate, $lte: endDate}
}
},
]
);

find document in today's date range in mongodb

I have a collection, Plans. I want to find everything within the range of one month. Here is a sample of what the data could look like:
{
"_id": "someid",
"dateStart": ISODate("2015-03-01T00:00:00Z"),
"dateEnd": ISODate("2015-03-31T00:00:00Z"),
"items": [
"someotherid"
]
}
How can I find the Plan such that today's date is between dateStart and dateEnd?
To find a Plan such that today's date is between the dateStart and dateEnd fields, create a date object that holds the current date then use the $lt and $gt query operators on the date fields to query documents where today's date falls between the two fields:
currentDate = new Date();
Plans.find({
dateStart: { $lt: currentDate },
dateEnd: { $gt: currentDate }
});
date1 = new Date('3/1/15');
date2 = new Date('4/1/15');
Plans.find({
startDate: {$gte: date1},
endDate: {$lt: date2}
});
New Date objects initialize to 00:00:000 so you can use that to your advantage here. Also note that "less than 4/1" is equivalent to "less than or equal to midnight on 3/31" - but the former is much easier to write.
Also note that moment is a super handy utility for doing manipulations like this. For example: moment().endOf('month').toDate()
To add the package just do:
$ meteor add momentjs:moment

Aggregate MongoDB results by ObjectId date

How can I aggregate my MongoDB results by ObjectId date. Example:
Default cursor results:
cursor = [
{'_id': ObjectId('5220b974a61ad0000746c0d0'),'content': 'Foo'},
{'_id': ObjectId('521f541d4ce02a000752763a'),'content': 'Bar'},
{'_id': ObjectId('521ef350d24a9b00077090a5'),'content': 'Baz'},
]
Projected results:
projected_cursor = [
{'2013-09-08':
{'_id': ObjectId('5220b974a61ad0000746c0d0'),'content': 'Foo'},
{'_id': ObjectId('521f541d4ce02a000752763a'),'content': 'Bar'}
},
{'2013-09-07':
{'_id': ObjectId('521ef350d24a9b00077090a5'),'content': 'Baz'}
}
]
This is what I'm currently using in PyMongo to achieve these results, but it's messy and I'd like to see how I can do it using MongoDB's aggregation framework (or even MapReduce):
cursor = db.find({}, limit=10).sort("_id", pymongo.DESCENDING)
messages = [x for x in cursor]
this_date = lambda x: x['_id'].generation_time.date()
dates = set([this_date(message) for message in messages])
dates_dict = {date: [m for m in messages if this_date(m) == date] for date in dates}
And yes, I know that the easiest way would be to simply add a new date field to each record then aggregate by that, but that's not what I want to do right now.
Thanks!
Update: There is a built in way to do this now, see https://stackoverflow.com/a/51766657/295687
There is no way to accomplish what you're asking with mongodb's
aggregation framework, because there is no aggregation operator that
can turn ObjectId's into something date-like (there is a JIRA
ticket, though). You
should be able to accomplish what you want using map-reduce, however:
// map function
function domap() {
// turn ObjectId --> ISODate
var date = this._id.getTimestamp();
// format the date however you want
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
// yields date string as key, entire document as value
emit(year+"-"+month+"-"+day, this);
}
// reduce function
function doreduce(datestring, docs) {
return {"date":datestring, "docs":docs};
}
The Jira Ticket pointed out by llovett has been solved, so now you can use date operators like $isoWeek and $year to extract this information from an ObjectId.
Your aggregation would look something like this:
{
"$project":
{
"_id": {
"$dateFromParts" : {
"year": { "$year": "$_id"},
"month": { "$month": "$_id"},
"day": { "$dayOfMonth": "$_id"}
}
}
}
}
So this doesn't answer my question directly, but I did find a better way to replace all that lambda nonsense above using Python's setdefault:
d = {}
for message in messages:
key = message['_id'].generation_time.date()
d.setdefault(key,[]).append(message)
Thanks to #raymondh for the hint in is PyCon talk:
Transforming Code into Beautiful, Idiomatic Python

How to query mongodb between dates only by ObjectID? [duplicate]

I know that ObjectIds contain the date they were created on. Is there a way to query this aspect of the ObjectId?
Popping Timestamps into ObjectIds covers queries based on dates embedded in the ObjectId in great detail.
Briefly in JavaScript code:
/* This function returns an ObjectId embedded with a given datetime */
/* Accepts both Date object and string input */
function objectIdWithTimestamp(timestamp) {
/* Convert string date to Date object (otherwise assume timestamp is a date) */
if (typeof(timestamp) == 'string') {
timestamp = new Date(timestamp);
}
/* Convert date object to hex seconds since Unix epoch */
var hexSeconds = Math.floor(timestamp/1000).toString(16);
/* Create an ObjectId with that hex timestamp */
var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
return constructedObjectId
}
/* Find all documents created after midnight on May 25th, 1980 */
db.mycollection.find({ _id: { $gt: objectIdWithTimestamp('1980/05/25') } });
In pymongo, it can be done this way:
import datetime
from bson.objectid import ObjectId
mins = 15
gen_time = datetime.datetime.today() - datetime.timedelta(mins=mins)
dummy_id = ObjectId.from_datetime(gen_time)
result = list(db.coll.find({"_id": {"$gte": dummy_id}}))
Using inbuilt function provided by mongodb drivers in in Node.js lets you query by any timestamp:
var timestamp = Date.now();
var objectId = ObjectID.createFromTime(timestamp / 1000);
Alternatively, to search for records before the current time, you can simply do:
var objectId = new ObjectID(); // or ObjectId in the mongo shell
Source: http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html
You can use $convert function to extract the date from ObjectId starting in 4.0 version.
Something like
$convert: { input: "$_id", to: "date" }
You can query on date comparing between start and end time for date.
db.collectionname.find({
"$expr":{
"$and":[
{"$gte":[{"$convert":{"input":"$_id","to":"date"}}, ISODate("2018-07-03T00:00:00.000Z")]},
{"$lte":[{"$convert":{"input":"$_id","to":"date"}}, ISODate("2018-07-03T11:59:59.999Z")]}
]
}
})
OR
You can use shorthand $toDate to achieve the same.
db.collectionname.find({
"$expr":{
"$and":[
{"$gte":[{"$toDate":"$_id"}, ISODate("2018-07-03T00:00:00.000Z")]},
{"$lte":[{"$toDate":"$_id"},ISODate("2018-07-03T11:59:59.999Z")]}
]
}
})
how to find Find the Command (this date[2015-1-12] to this Date[2015-1-15]):
db.collection.find({
_id: {
$gt: ObjectId(Math.floor((new Date('2015/1/12'))/1000).toString(16) + "0000000000000000"),
$lt: ObjectId(Math.floor((new Date('2015/1/15'))/1000).toString(16) + "0000000000000000")
}
}).pretty()
Count the Command (this date[2015-1-12] to this Date[2015-1-15]):
db.collection.count({
_id: {
$gt: ObjectId(Math.floor((new Date('2015/1/12'))/1000).toString(16) + "0000000000000000"),
$lt: ObjectId(Math.floor((new Date('2015/1/15'))/1000).toString(16) + "0000000000000000")
}
})
Remove the Command (this date[2015-1-12] to this Date[2015-1-15]):
db.collection.remove({
_id: {
$gt: ObjectId(Math.floor((new Date('2015/1/12'))/1000).toString(16) + "0000000000000000"),
$lt: ObjectId(Math.floor((new Date('2015/1/15'))/1000).toString(16) + "0000000000000000")
}
})
Since the first 4 bytes of an ObjectId represent a timestamp, to query your collection chronologically, simply order by id:
# oldest first; use pymongo.DESCENDING for most recent first
items = db.your_collection.find().sort("_id", pymongo.ASCENDING)
After you get the documents, you can get the ObjectId's generation time like so:
id = some_object_id
generation_time = id.generation_time
Yes you can query object by date using MongoDB inserted ID
db.collectionname.find({_id: {$lt: ObjectId.fromDate( new ISODate("TZformat") ) } });
let's suppose users is my collection and I want all users created less than 05 January 2018
db.users.find({_id: {$lt: ObjectId.fromDate( new ISODate("2018-01-05T00:00:00.000Z") ) } });
For running from a query we can use like
db.users.find({_id: {$lt: ObjectId.fromDate(new Date((new Date().getTime() - (1 * 3 * 60 * 60 * 1000))) ) } })
All the users from the current time - 3 hours
To get last 60 days old documents in mongo collection i used below query in shell.
db.collection.find({_id: {$lt:new ObjectId( Math.floor(new Date(new Date()-1000*60*60*24*60).getTime()/1000).toString(16) + "0000000000000000" )}})
If you want to make a range query, you can do it like in this post. For example querying for a specific day (i.e. Apr 4th 2015):
> var objIdMin = ObjectId(Math.floor((new Date('2015/4/4'))/1000).toString(16) + "0000000000000000")
> var objIdMax = ObjectId(Math.floor((new Date('2015/4/5'))/1000).toString(16) + "0000000000000000")
> db.collection.find({_id:{$gt: objIdMin, $lt: objIdMax}}).pretty()
From the documentation:
o = new ObjectId()
date = o.getTimestamp()
this way you have date that is a ISODate.
Look at
http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Extractinsertiontimesfromidratherthanhavingaseparatetimestampfield.
for more information
Using MongoObjectID you should also find results as given below
db.mycollection.find({ _id: { $gt: ObjectId("5217a543dd99a6d9e0f74702").getTimestamp().getTime()}});
A Solution Filtering within MongoDB Compass.
Based on versions:
Compass version: 1.25.0
MongoDB version: 4.2.8
Option 1:
#s7vr 's answer worked perfectly for me. You can paste this into the Filter field:
{$expr: { $and: [ {$gte: [{$toDate: "$_id"}, ISODate('2021-01-01')]}, {$lt: [{$toDate: "$_id"}, ISODate('2021-02-01')]} ] } }
Option 2:
I also found this to work (remember that the Date's month parameter is 0-based indexing so January is 0):
{_id: {$gte: ObjectId(Date(2021, 0, 1) / 1000), $lt: ObjectId(Date(2021, 1, 1) / 1000) } }
Option 3:
Equivalent with ISODate:
{_id: {$gte: ObjectId(ISODate('2021-01-01') / 1000), $lt: ObjectId(Date('2021-02-01') / 1000) } }
After writing this post, I decided to run the Explain on these queries. Here's the skinny on performance:
Option 1: 39 ms, 0 indexes used, 30 ms in COLLSCAN
Option 2: 0 ms, _id index used
Option 3: 1 ms, _id index used, 1 ms in FETCH
Based on my rudimentary analysis, it appears that option 2 is the most efficient. I will use Option 3, personally, as it is a little cleaner to use ISODate rather than remembering 0-based month indexing in the Date object.
In rails mongoid you can query using
time = Time.utc(2010, 1, 1)
time_id = ObjectId.from_time(time)
collection.find({'_id' => {'$lt' => time_id}})