I am looking to create a query within the scope that will search for records that have a date less than or equal to the current date. I understand how to do this in SQL but I'm not sure how this would translate over to this. Currently all I have is this
scope :index_overdue, -> { where(active: true, end_date: nil).order(arel_table[:complete_by], arel_table[:latest_update_date], arel_table[:id]) }
I need the query to only retrieve records that have a complete_by date that is <= The current date.
I would use a beginless range to write that condition:
scope :index_overdue, -> {
where(active: true, end_date: nil, complete_by: ..Date.today)
.order(...)
}
Related
Background:
We are trying to identify if notifications are due to go out against records that have configurable schedules. So, for instance, a record could have:
NotificationStep: 'week(s)'
NotificationCount: 3
StartDate: 2018-11-17
This would signal that 3 weeks after Nov 17 -- or the last time one of these notifications went out -- we need to send out a new notification.
In SQL this would involve comparing today to subquery (and possibly a union) utilizing the MAX() method.
Question:
Is there a good way to use today's date as the value by which you're comparing your calculated values against?
Something like:
myModel.find({
where:{
new Date(): {$gt: Notifications.max('date', {where: ... }
}
});
All you need is sequelize.where and sequelize.fn
Here you go :
myModel.find({
where:
sequelize.where(sequelize.fn('now') , {$gt: Notifications.max('date', {where: ... } )
});
Note : sequelize.fn('now') // <---- Give you current time from DB
My goal is to get the statistics count of data through timestamp, some what like google analytics. I'm using node.js & sequelize The timestamp field in model looks
request_timestamp: {
type: DataTypes.STRING,
allowNull: false
},
For now, I want to get data grouped by day of every timestamp. I tries date_truc but it's not working for some reason.
Let's say I have the following documents
Document 1
{
companyId: "1",
salesDate: "1425254400000" //this is UTC time as a long
}
Document 2
{
companyId: "1",
salesDate: "1425340800000" //this is UTC time as a long
}
Document 3
{
companyId: "2",
salesDate: "1425254400000" //this is UTC time as a long
}
I currently have my view set up as
function(doc, meta) { emit([doc.salesDate, doc.companyId], doc); }
Which is pulling back all 3 documents when using
?startkey=[1425254400000,"1"]&endkey=[1425340800000,"1"]
I'm not sure how to make it only pull back the sales for that date range by company id.
The sql version would be SELECT * FROM sales WHERE companyId = :companyId AND salesDate BETWEEN :rangeStart AND :rangeEnd
EDIT: I'm using the rest API.
When designing views for range queries with multiple query fields, the fixed query field(companyId) should be a prefix of the compound index and the range query field should be at the end. With the current view, Couchbase will emit every document where salesDate is within the range without considering companyId.
Reversing the order of keys will work:
function(doc, meta) {
emit([doc.companyId, doc.salesDate], doc);
}
Query:
?startkey=["1", 1425254400000]&endkey=["1", 1425340800000]
N.B. if salesDate is a string and not a numeric value, Couchbase will use lexicographic ordering to perform the range query.
I have a use-case where I want to search all the SQL queries on an entity in database, and later I want to query on which fields and for which values that query had been executed. I am developing this in ruby.
For example, Following queries were ran:
select * from entity where created_at > "2015-03-01" and created_at < "2015-03-03" ;
select * from entity where created_at > "2015-03-03" and created_at < "2015-03-04" and type in ('forward');
select * from entity where created_at > "2015-03-01" and created_at < "2015-03-03" and type in ('reverse');
Later on, I should be able to fetch for which dates on created_at, these queries were ran. This can be queried for type as well, so want to keep it completely generic.
So, I was thinking to save conditions of these queries in JSON format. Let me know if you know of any format or gem, which can help for such case.
I was thinking of constructing JSON for these queries in following format:
{
"query": [
{
"field": "created_at",
"value_more_than": "2015-03-03",
"value_less_than": "2015-03-04",
"type" : "date"
},
{
"field": "type",
"value_in": "forward",
"type" : "String"
}
]
}
And saving these for future reference and querying on these when asked for. Please let me know, of you are familiar with a better format or can suggest some improvement overall.
This question already has answers here:
Count of records by Date MongoDB
(5 answers)
Closed 2 years ago.
How do i convert the below mysql query to mongodb query: SELECT count(*) as count , DATE_FORMAT( timestamp, '%d-%c-%Y' ) as day, timestamp as tm FROM visits WHERE 1 GROUP BY day ORDER BY tm. I want to use this on a nodejs so i am using native mongodb.
Get the number of pageviews for each day in mongodb where each pageview is stored along with the timestamp.
Your question lacks any effort on your part and we rarely just "give" people the answer like this, however, this one time:
NB: you cannot yet manipulate dates to cast them to different formats without some manual work yourself of picking the parts out and rejoining them. Because of this I have left out the date formatting you did and just used it as an object.
db.visits.aggregate([
{
$project: {
date: {day: {$dayOfMonth: '$timestamp'}, month: {$month: '$timestamp'}, year: {$year: '$timestamp'}},
//day: {concat: [date.day,date.mont,date.year]}
}
},
{$group: {_id: '$date', tm: '$timestamp', count: {$sum:1}}}
])
I found a working mongodb query using mapreduce which gives me the output time as unix time rather than the format I had mentioned in the question. But this was the query that was sorting the time properly. I had tried mongo group query but it did not sort according to time. The working mongo query is :
db.visits.mapReduce(
function(){
day = Date.UTC(this.timestamp.getFullYear(), this.timestamp.getMonth(), this.timestamp.getDate());
emit({day: day}, {count: 1});
},
function(key, values) {
var count = 0;
values.forEach(function(v) {
count += v['count'];
});
return {count: count};
},
{
out : {inline:1},
sort:{_id:1}
}
);