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);
Related
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.
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' ]
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.
I have this collection
> db.test.find()
{ "_id" : ObjectId("5398ddf40371cdb3aebca3a2"), "name" : "ahmed", "qte" : 30 }
{ "_id" : ObjectId("5398de040371cdb3aebca3a3"), "name" : "demha", "qte" : 35 }
{ "_id" : ObjectId("5398de140371cdb3aebca3a4"), "name" : "ahmed", "qte" : 50 }
{ "_id" : ObjectId("5398de210371cdb3aebca3a5"), "name" : "ahmed", "qte" : 60 }
i would like to sum "qte" where "name"= "ahmed" and print the sum with php
i know how to do with SQL but i have no idea how it is in mongodb.
Thanks :)
Use the aggregation framework.
Assuming you have an the current collection as $collection
result = $collection->aggregate(array(
array(
'$match' => array(
'name' => 'ahmed'
)
),
array(
'$group' => array(
'_id' => NULL,
'total' => array(
'$sum' => '$qte'
)
)
)
));
The two parts are the $match to meet the criteria, and the $group to arrive at the "total" using $sum
See other Aggregation Framework Operators and the Aggregation to SQL Mapping chart for more examples.
This is done with an aggregate statement:
db.test.aggregate([
{
$match: {
name: "ahmed"
}
},
{
$group: {
_id:"$name",
total: {
$sum: "$qte"
}
}
}
])
I'm really stuck with this problem like 6 hours ago. It's really frustrating.
This is my collection:
{
"_id" : ObjectId("52fbcd2aa0a9210007000038"),
"from_group" : "52faa17fa0a921740c000046",
"to_group" : "52faa173a0a921c807000037",
"message" : "How are you?",
"sent_time" : ISODate("2014-02-12T19:36:10.000Z"),
}
{
"_id" : ObjectId("52fbcd39a0a921641200002c"),
"from_group" : "52faa173a0a921c807000037",
"to_group" : "52faa17fa0a921740c000046",
"message" : "Fine, and you?",
"sent_time" : ISODate("2014-02-12T19:36:25.000Z"),
}
{
"_id" : ObjectId("52fbcd4ea0a9210007000039"),
"from_id" : "1305426002",
"from_group" : "52faa17fa0a921740c000046",
"to_group" : "52faa173a0a921c807000037",
"message" : "Fine",
"sent_time" : ISODate("2014-02-12T19:36:46.000Z"),
}
{
"_id" : ObjectId("52fbfa2aa0a921641200002d"),
"from_group" : "52fbfa1ca0a921f81300002a",
"to_group" : "52faa173a0a921c807000037",
"message" : "Test... 1.. 2.. 3..",
"sent_time" : ISODate("2014-02-12T22:48:10.000Z"),
}
This is my code:
function getLastMessage($hangoutA, $hangoutB) {
$result = $mongodb->messages->aggregate(
['$match' =>
['$or' =>
[
array('from_group' => $hangoutA),
array('from_group' => $hangoutB),
array('to_group' => $hangoutA),
array('to_group' => $hangoutB),
]
]
],
[ '$group' => [
'_id' => null,
'message' => ['$last' => '$message']
]
]);
$message = $result;
return $message;
}
$first = getLastMessage("52faa173a0a921c807000037", "52fbfa1ca0a921f81300002a");
$second = getLastMessage("52faa173a0a921c807000037", "52faa17fa0a921740c000046");
$first and $second return this:
Array ( [result] => Array ( [0] => Array ( [_id] => [message] => Test... 1.. 2.. 3.. ) ) [ok] => 1 )
The problem with $first is that the message that I expect is: "Fine".
What I expect for $second is: "Test... 1.. 2.. 3..", so that is OK.
Can you help me with this problem?
The problem was that I try to check all the combinations. That is a st*pid error.
To get the result that I was expected, I change this:
array('from_group' => $hangoutA),
array('from_group' => $hangoutB),
array('to_group' => $hangoutA),
array('to_group' => $hangoutB),
for this
array('to_group' => $hangoutA),
array('to_group' => $hangoutB),
Easy, right?
Sorry!
This seems like a simple query to me that doesn't need the aggregation framework. (Are you using the aggregation framework for some specific reason?)
I'm not into php so I'll use the mongo console syntax.
To find the last message sent to a group:
db.messages.find({"to_group" : groupID}).sort({"sent_time" : -1}).limit(1)
To find the last message between 2 groups:
db.messages.find({"$or" : [{"to_group" : groupA, "from_group" : groupB}, {"to_group" : groupB, "from_group" : groupA}]}).sort({"sent_time" : -1}).limit(1)