Compare hour of dates in mongo find method - mysql

I have mongo documents containing a field createAt date. I would like to search for all documents where
the hour of createAt at 8 o'clock in the morning of everyday (between 8:00am and 8:00am)
, but I have no clue how to write the query.
In MySQL I can write it like this:
select * from table where hour(createAt)= 8

You can use $hour with $expr.
db.collection.find({
$expr: {
$eq: [
{
$hour: "$createdAt"
},
8
]
}
})
Sample Mongo Playground

Your question has already been resolved.
I wrote you an example on the playground.

Related

Get the latest year in MongoDB

While working on a project, I came across a problem that I can't seem to find the solution for anywhere.
I'm working with NoSQLBooster for MongoDB and I need to fetch the most recent year present in my data.
I don't want to sort it in a descending way (doing so shows me all the values, and I can see that the year 2018 is the latest), but I want it to just present the number 2018.
Does anyone know how I can do this?
Thank you in advance!
This is pretty fast:
var x= db.find({},{_id:1}).sort(_id:-1).limit(1)
x._id.getTimestamp()
You can use $group (aggregation)
db.aggregate(
[
{
$group :
{
_id : null,
maxYear: { $max: "$yearField" }
}
}
]
)

How do I query a complex JSONB field in Django 1.9

I have a table item with a field called data of type JSONB. I would like to query all items that have text that equals 'Super'. I am trying to do this currently by doing this:
Item.objects.filter(Q(data__areas__texts__text='Super'))
Django debug toolbar is reporting the query used for this is:
WHERE "item"."data" #> ARRAY['areas', 'texts', 'text'] = '"Super"'
But I'm not getting back any matching results. How can I query this using Django? If it's not possible in Django, then how can I query this in Postgresql?
Here's an example of the contents of the data field:
{
"areas": [
{
"texts": [
{
"text": "Super"
}
]
},
{
"texts": [
{
"text": "Duper"
}
]
}
]
}
try Item.objects.filter(data__areas__0__texts__0__text='Super')
it is not exact answer, but it can clarify some jsonb filter features, also read django docs
I am not sure what you want to achieve with this structure, but I was able to get the desired result only with strange raw query, it can look like this:
Item.objects.raw("SELECT id, data FROM (SELECT id, data, jsonb_array_elements(\"table_name\".\"data\" #> '{areas}') as areas_data from \"table_name\") foo WHERE areas_data #> '{texts}' #> '[{\"text\": \"Super\"}]'")
Dont forget to change table_name in query (in your case it should be yourappname_item).
Not sure you can use this query in real programs, but it probably can help you to find a way for a better solution.
Also, there is very good intro to jsonb query syntax
Hope it will help you

MySQL query to retrieve tabular data in json format

I have a table like below in MySQl database
user-name mail
ganesh g#g.com
gani gani#gani.com
gan gan#gan.com
I need query to retrieve above table in JSON format
Example:
[{
user-name:"ganesh",
mail:"g#g.com"
},
{
user-name:"gani",
mail:"gani#gani.com"
},
{
user-name:"gan",
mail:"gan#gan.com"
}
]
I need help, to do above
It's not recommended to do such things in the DBMS, do it in the script that is loading the data instead, if you're wrapping some legacy code you can't edit then wrap it with more code to format the data.
If all that fails do something like this: http://www.thomasfrank.se/mysql_to_json.html
SELECT
CONCAT("[",
GROUP_CONCAT(
CONCAT("{username:'",username,"'"),
CONCAT(",email:'",email),"'}")
)
,"]")
AS json FROM users;

MongoDB equivalent to NOW() + INTERVAL

I'm currently working on converting our PHP backend from MySQL to MongoDB.
We are often using something like this in MySQL:
UPDATE table_1 SET completion_time = NOW() + INTERVAL 90 MINUTE WHERE id = 1;
How would I do this in MongoDB? Do I need to use 2 queries? First query to set completion_time with $currentDate and the 2nd query to increment it? I've read that $inc doesn't work on Dates in MongoDB, tho ...
You could try creating a date variable that holds the current date + 90 minutes later which you can then use to set the completion_time field with in your update:
var ninetyMinutesLater = new Date();
ninetyMinutesLater.setMinutes(ninetyMinutesLater.getMinutes() + 90);
db.table_1.update(
{ "_id": 1 },
{
"$set": {
"completion_time": ninetyMinutesLater
}
}
);
I see you have have tagged this with mongodb-php so I assume you are using PHP. MongoDB does have a $currentDate operator but currently there is no way to actually set an offset to that date.
For example I tried #diwakar's answer on 3.0:
> db.table_1.update({"id":1 },{completion_time: { $add: [ "$currentDate", 90 ] }})
2015-03-23T11:22:05.497+0000 E QUERY Error: field names cannot start with $ [$add]
at Error (<anonymous>)
at DBCollection._validateForStorage (src/mongo/shell/collection.js:160:19)
at DBCollection._validateForStorage (src/mongo/shell/collection.js:164:18)
at DBCollection._validateUpdateDoc (src/mongo/shell/collection.js:387:14)
at Object.findOperations.updateOne (src/mongo/shell/bulk_api.js:675:20)
at DBCollection.update (src/mongo/shell/collection.js:454:22)
at (shell):1:12 at src/mongo/shell/collection.js:160
So currently this needs to be done client side like so:
$mongo->collection->insert(['completion_time' => new MongoDate(time() + (60*90))])
It seems that is the only way.
Starting with MongoDB v5.0+, you can use $dateAdd with $$NOW.
db.collection.update({
_id: 1
},
[
{
$set: {
completion_time: {
"$dateAdd": {
"startDate": "$$NOW",
"unit": "minute",
"amount": 90
}
}
}
}
])
Here is the Mongo Playground for your reference.
use
db.collection.UpdateOne({ _id:id},{
$set:{
"completion_time": $add:[ $currentDate , (60*90)]
}
})
Please try this definitely work,
db.table_1.update({"id":1 },{completion_time: { $add: [ "$date", 90 ] }})

Get count for each day in mongodb using aggregate or map reduce [duplicate]

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}
}
);