Mongodb string to date conversion - php

Below is my sample mongodb collection
{
"_id" : ObjectId("57ed32f4070577ec56a56b9f"),
"log_id" : "180308",
"issue_id" : "108850",
"author_key" : "priyadarshinim_contus",
"timespent" : NumberLong(18000),
"comment" : "Added charts in the dashboard page of the application.",
"created_on" : "2017-08-16T18:22:04.816+0530",
"updated_on" : "2017-08-16T18:22:04.816+0530",
"started_on" : "2017-08-16T18:21:39.000+0530",
"started_date" : "2017-08-02",
"updated_date" : "2017-08-02",
"role" : "PHP",
"updated_at" : ISODate("2017-09-29T15:27:48.069Z"),
"created_at" : ISODate("2017-09-29T15:27:48.069Z"),
"status" : 1.0
}
I need to get records with help of started_date , by default I will give two dates in that i will check $gt and $lt of started date .
$current_date = '2017-08-31';
$sixmonthfromcurrent ='2017-08-01';
$worklogs = Worklog::raw ( function ($collection) use ($issue_jira_id, $current_date, $sixmonthfromcurrent) {
return $collection->aggregate ( [
['$match' => ['issue_id' => ['$in' => $issue_jira_id],
'started_date' => ['$lte' => $current_date,'$gte' => $sixmonthfromcurrent]
]
],
['$group' => ['issue_id' => ['$push' => '$issue_id'],
'_id' => ['year' => ['$year' => '$started_date'],
'week' => ['$week' => '$started_date'],'resource_key' => '$author_key'],
'sum' => array ('$sum' => '$timespent')]
],
[ '$sort' => ['_id' => 1]
]
] );
} );
If I run this query I am getting this type of error:
Can't convert from BSON type string to Date
How to rectify this error?

The only field in your $group that I see as troubling is the field week.
The year you could extract by doing a $project before your $group aggregation:
$project: {
year: { $substr: [ "$started_date", 0, 4 ] },
issue_id: 1,
author_key: 1,
timespent: 1
}
if you know that the date string will always come at this format. Of course you cannot do a substr operation for finding out the week.
It would be easy though if your field started_date would be an actual ISODate(), then you could use exactly what you wrote as you probably already saw in the documentation.
If you need the field week very bad, which I imagine you do, then I'd suggest you convert your field started_date to an ISODate().
You can do that with a bulkWrite:
db = db.getSiblingDB('yourDatabaseName');
var requests = [];
db.yourCollectionName.find().forEach(doc => {
var date = yourFunctionThatConvertsStringToDate(doc.started_date);
requests.push( {
'updateOne': {
'filter': { '_id': doc._id },
'update': { '$set': {
"started_date": date
} }
}
});
if (requests.length === 500) {
db.yourCollectionName.bulkWrite(requests);
requests = [];
}
});
if(requests.length > 0) {
db.yourCollectionName.bulkWrite(requests);
}
Load this script directly on your mongodb server and execute there.
Hope this helps.

Related

PHP MongoDB getting data from a date range using dateFromString

I have data stored in the following format in MongoDB:
[
{
meta: {
id: 1
},
data: {
date: "03/01/2020"
}
}
],
[
{
meta: {
id: 1
},
data: {
date: "12/19/2019"
}
}
]
And I want to use PHP to get all entries that are greater than or equal to the current date, and less than or equal to the current date plus 6 months.
To do that, my query looks like this:
$query = [
'meta.id' => 1,
'$expr' => [
'$and' => [
[
'$gte' =>
[
['$dateFromString' => ['dateString' => '$data.date']],
time()
]
],
[
'$lte' =>
[
['$dateFromString' => ['dateString' => '$data.date']],
strtotime('+6 months')
]
]
]
]
];
With this query, I should get back the first entry, with a date of "03/01/2020", however I get back no results.
I've also tried using 'format' in the dateString but it doesn't appear to be supported yet, as it gives me an error.
How can I get this query to work correctly?

MongoDB using UTCDateTime in $gte doesn't work

I'm writing a query to search for users created at specific date e.g.
$date = '2018-05-02 15:46:41.000Z';
User::raw(function ($col) use($date) {
return $col->aggregate([
['$match' =>
'created_at' => ['$gte' => new \MongoDB\BSON\UTCDateTime(strtotime($date))]
]
]);
});
Given an sample data like
{
"_id" : ObjectId("5ae9dd61ef7a5754f159c656"),
"name" : "Effie Larkin",
"created_at" : ISODate("2018-05-02 15:46:41.000Z"),
}...
Expected result should be
{
"users" : [
"_id" : {"$oid": "5ae9dd61ef7a5754f159c656"},
"name" "Effie Larkin",
"created_at" : "2018-05-02 15:46:41"
]
}...
But I get an empty json array []. I think the problem is in UTCDateTime function but I don't how to pass dateTime to it. I want to pass a time interval like 1 week ago or yesterday etc... if anyone can help me will be much appreciated, thanks
Update, the example below fixed it
$date = '2018-05-02';
User::raw(function ($col) use($date) {
return $col->aggregate([
['$match' =>
'created_at' => ['$gte' => new \MongoDB\BSON\UTCDateTime(strtotime($date)*1000)]
]
]);
});

Trouble with converting working MongoDB aggregation pipeline query into PHP

I'm using MongoChef to construct an aggregation pipeline command that performs a $match, then a $group then a $project.
The following code produces the correct output and is confirmed working in MongoDB itself:
db.collection_name.aggregate(
[
{
$match: {
":energy_mon_id" : 9
}
},
{
$group: {
"_id" : {
"Date" : "$:date"
},
"avg_voltage_a" : {
"$avg" : "$:voltage_a"
},
"avg_voltage_b" : {
"$avg" : "$:voltage_b"
},
"avg_voltage_c" : {
"$avg" : "$:voltage_c"
}
}
},
{
$project: {
"avg_volt" : {
"$add" : [
"$avg_voltage_a",
"$avg_voltage_b"
]
}
}
},
{
$match: {
}
}
]
);
The output that is produced is this (note that the avg_volt values are integers):
{ "_id" : { "Date" : "2016-06-06" }, "avg_volt" : 779 }
{ "_id" : { "Date" : "2016-06-08" }, "avg_volt" : 779 }
Now the issue I'm faced with is getting this to run correctly in PHP.
My code in PHP is below:
$collection = $this->db->testdb->collection_name;
$aggrCommand = array(
array(
'$match' => array( ":energy_mon_id" => 9)
),
array(
'$group' => array(
"_id" => array(
"Date" => "$:date"
),
"avg_voltage_a" => array(
'$avg' => "$:voltage_a"
),
"avg_voltage_b" => array(
'$avg' => "$:voltage_b"
),
"avg_voltage_c" => array(
'$avg' => "$:voltage_c"
),
)
),
array(
'$project' => array(
"avg_volt" => array(
'$add' => ["$avg_voltage_a","$avg_voltage_b" ]
)
)
)
);
$list = $collection->aggregate( $aggrCommand);
The error I get is for this very last line when I try to submit a GET request via postman:
Slim Application Error
The application could not run because of the following error:
Details
Type: MongoDB\Driver\Exception\RuntimeException
Code: 16554
Message: $add only supports numeric or date types, not String
File: C:\wamp\www\DRM\vendor\mongodb\mongodb\src\Operation\Aggregate.php
Line: 168
This makes no sense at all since the output in MongoDB is an integer value, and I can't spot any errors in my conversion to PHP.
One thing to note is that my field names do actually contain a colon at the front (its not a mistake), but I don't think that is the issue here.
I'm really stumped with this, can someone provide any advice on how to figure this issue out? The output from my MongoDB command is clearly not a string, yet in PHP it says the values of avg_voltage_a and avg_voltage_b are strings. This makes no sense at all.
Try with single quotes in your "$add" statement to prevent PHP interpreting "$avg_voltage_a" as a variable :
'$add' => ['$avg_voltage_a','$avg_voltage_b' ]

how to aggregate mongodb collection data in laravel

i have collection like this
{
"wl_total" : 380,
"player_id" : 1241,
"username" : "Robin",
"hand_id" : 292656,
"time" : 1429871584
}
{
"wl_total" : -400,
"player_id" : 1243,
"username" : "a",
"hand_id" : 292656,
"time" : 1429871584
}
as both collection have same hand_id i want to aggregate both these collection on the basis of hand_id
i want result as combine of
data=array(
'hand_id'=>292656,
'wl_total'=>
{
0=>380,
1=>-400
},
'username'=>
{
0=>"Robin",
1=>"a"
},
"time"=>1429871584
)
You basically want a $group by the "hand_id" common to all players, and then $push to different arrays in the document and then also do something with "time", I took $max. Nees to be an accumulator of some sort at any rate.
Also not sure what your underlying collection name is, but you can call this in laravel with a construct like this:
$result = DB::collection('collection_name')->raw(function($collection)
{
return $collection->aggregate(array(
array(
'$group' => array(
'_id' => '$hand_id',
'wl_total' => array(
'$push' => '$wl_total'
),
'username' => array(
'$push' => '$username'
),
'time' => array(
'$max' => '$time'
)
)
)
));
});
Which returns output ( shown in json ) like this:
{
"_id" : 292656,
"wl_total" : [
380,
-400
],
"username" : [
"Robin",
"a"
],
"time" : 1429871584
}
Personally I would have gone for a single array with all the infomation in it for the grouped "hand", but I supose you have your reasons why you want it this way.

PHP & MongoDB show results grouped by date

I have an mongodb collection with following documents:
{
"_id" : ObjectId("547af6aea3f0eba7148b4567"),
"check_id" : "f5d654e7-257d-4a93-ae50-2d59dfeeb451",
"chunks" : NumberLong(200),
"num_hosts" : NumberLong(1000),
"num_rbls" : NumberLong(163),
"owner" : NumberLong(7901),
"created" : ISODate("2014-11-30T10:51:26.924Z"),
"started" : ISODate("2014-11-30T10:51:31.558Z"),
"finished" : ISODate("2014-11-30T10:57:08.512Z")
}
{
"_id" : ObjectId("54db19a858a5d395a18b4567"),
"check_id" : "9660e510-1349-43f3-9d5e-8bf4b06179be",
"chunks" : NumberLong(2),
"num_hosts" : NumberLong(10),
"num_rbls" : NumberLong(166),
"owner" : NumberLong(7901),
"created" : ISODate("2015-02-11T08:58:17.118Z"),
"started" : ISODate("2015-02-11T08:58:18.78Z"),
"finished" : ISODate("2015-02-11T08:58:47.486Z")
}
{
"_id" : ObjectId("54db267758a5d30eab8b4567"),
"check_id" : "9660e510-1349-43f3-9d5e-8bf4b06179be",
"chunks" : NumberLong(2),
"num_hosts" : NumberLong(10),
"num_rbls" : NumberLong(166),
"owner" : NumberLong(7901),
"created" : ISODate("2015-02-11T09:52:55.388Z"),
"started" : ISODate("2015-02-11T09:52:56.109Z"),
"finished" : ISODate("2015-02-11T09:53:22.095Z")
}
What I need is to get the result and produce an array similar to this:
Array
(
[2015-02-11] => array
(
//array with results from 2015-02-11
)
[2014-11-30] => array
(
//array with results from 2014-11-30
)
)
I know that it's possible to just perform simply collection->find and then loop through results and use php logic to achieve my goal but is it possible to make it using mongo? Maybe using aggregation framework?
EDIT: I want to group results by "created" date
Any help will be highly appreciated.
Monogo aggregation mongo aggregation group used for this, so below query may solve your problem
db.collectionName.aggregate({
"$group": {
"_id": "$created",
"data": {
"$push": {
"check_id": "$check_id",
"chunks": "$chunks",
"num_hosts": "$num_hosts",
"num_rbls": "$num_rbls",
"owner": "$owner",
"started": "$started",
"finished": "$finished"
}
}
}
}).pretty()
Or
db.collectionName.aggregate({
"$group": {
"_id": "$created",
"data": {
"$push": "$$ROOT"
}
}
}).pretty()
Also in mongo 2.8 $dateToString provide facility to convert ISO date to string format so below query also work
db.collectionName.aggregate([
{
"$project": {
"yearMonthDay": {
"$dateToString": {
"format": "%Y-%m-%d",
"date": "$created"
}
},
"check_id": "$check_id",
"chunks": "$chunks",
"num_hosts": "$num_hosts",
"num_rbls": "$num_rbls",
"owner": "$owner",
"started": "$started",
"finished": "$finished"
}
},
{
"$group": {
"_id": "$yearMonthDay",
"data": {
"$push": "$$ROOT"
}
}
}
]).pretty()
I have managed to solve this using the aggregation framework. Here is the answer, in case anyone need it.
$op = array(
array(
'$project' => array(
'data' => array(
'check_id' => '$check_id',
'chunks' => '$chunks',
'num_hosts' => '$num_hosts',
'num_rbls' => '$num_rbls',
'owner' => '$owner',
'started' => '$started',
'finished' => '$finished',
),
'year' => array('$year' => '$created' ),
'month' => array('$month' => '$created' ),
'day' => array('$dayOfMonth' => '$created'),
)
),
array(
'$group' => array(
'_id' => array('year' => '$year', 'month' => '$month', 'day' => '$day'),
'reports_data' => array('$push' => '$data'),
)
),
);
$c = $collection->aggregate($op);

Categories