MongoDB query in PHP

174 views
Skip to first unread message

Sayta Dommeti

unread,
Aug 9, 2014, 6:49:14 AM8/9/14
to mongod...@googlegroups.com

Hi All,

I am trying to export mongod data into a flat file in a specific format using PHP. To do this, I am using following aggregate query in PHP script. I have basic knowledge to use mongo query in PHP script. But, I got below exception while running PHP application.  Can anyone please help me to resolve this issue ?

From GenReport Cath Exception catched:
Code: 16021Message: Host:27017: exception: the $concat operator does not accept an object as an operand


MongoDB Query:

db.ohpublic.aggregate([{$match:{ts:{'$gte':ISODate('2014-08-04T00:00:00.000Z'),'$lt':ISODate('2014-08-05T00:00:00.000Z')},
'$or':[{'$and':[{channelType:'SMS'},{requestType:'INBOUND'},{eventId:{$in:['SMS_MO',null]}}]},
{'$and':[{apiName:{$in:['VOICE_IN_BOUND','VOICE_IN_BOUND_REJECT','VIRTUAL_NUMBER']}},{eventId:'release'}]},
{'$and':[{channelType:'SMS'},{requestType:'OUTBOUND'},{eventId:{$in:['SMS_MT','SMS_MT_PRO']}}]},
{'$and':[{channelType:'VOICE'},{requestType:'OUTBOUND'},{eventId:'release'}]}]}},
{$group:{_id:{day:{$dayOfMonth:'$ts'},month:{$month:'$ts'},year:{$year:'$ts'},userId:'$userId',serviceType:'$serviceType',status:'$status',
'Service':{'$concat':[
    {'$cond':[{$and:[{$eq:['$channelType','SMS']},{$eq:['$requestType','OUTBOUND']},{'$or':[{$eq:['$eventId','SMS_MT']},{$eq:['$eventId','SMS_MT_PRO']}]}]},'SMS_MT','']},
    {'$cond':[{$and:[{'$or':[{$eq:['$apiName','VOICE_IN_BOUND']},{$eq:['$apiName','VIRTUAL_NUMBER']},{$eq:['$apiName','VOICE_IN_BOUND_REJECT']}]},{$eq:['$eventId','release']}]},'VOICE_IN_BOUND','']},
    {'$cond':[{'$and':[{$eq:['$channelType','SMS']},{$eq:['$requestType','INBOUND']},{$or:[{'$eq':['$eventId','SMS_MO']},{'$eq':['$eventId',null]}]}]},'SMS_MO','']},
    {'$cond':[{$and:[{$eq:['$channelType','VOICE']},{$eq:['$requestType','OUTBOUND']},{$eq:['$eventId','release']}]},'VOICE_OUT_BOUND','']}]}},count:{$sum:1}}},
    ])


PHP Script:


$cursor=$db->ohpublic->aggregate(array(

    array(
                '$match' => array(
                    'ts' => array('$gte' => $start, '$lt' => $end),'$or'=>array(array(
                    '$and'=> array(array('channelType'=>'SMS','requestType'=>'INBOUND','eventId'=>array('$in' =>  array('SMS_MO',null)))),
                    '$and' => array(array('apiName' => array('$in' => array('VOICE_IN_BOUND','VOICE_IN_BOUND_REJECT','VIRTUAL_NUMBER')),array('eventId' => 'release'))),
                    '$and' => array(array('channelType' => 'SMS','requestType' => 'OUTBOUND','eventId' => array('$in' => array('SMS_MT','SMS_MT_PRO')))),
                    '$and' => array(array('channelType' => 'VOICE','requestType' => 'OUTBOUND','eventId' => 'release'))))
                   
                     ),
            ),

          
    array(
                '$group' => array(
                '_id' => array('day' => array('$dayOfMonth' => '$ts'),'month' => array('$month' => '$ts'),'year' => array('$year' => '$ts'), 'userId' => '$userId', 'serviceType' => '$serviceType', 'status' => '$status',
                    'Service'=> array(
                    '$concat' => array(
                    '$cond' => array('$and' => array(array('$eq' => array('$channelType' => 'SMS'),array('$eq' => array('$requestType' => 'OUTBOUND')),
                                        array('$or' => array(array('$eq' => array('$eventId'=>'SMS_MT'))),array('$eq' => array('$eventId' => 'SMS_MT_PRO'))))),'SMS_MT',''),
                   
                    '$cond' => array('$and' => array(array('$or' => array(array('$eq'=> array('$apiName'=>'VOICE_IN_BOUND'),array('$eq' => array('$apiName' => 'VIRTUAL_NUMBER'),
                                        array('$eq' => array('$apiName' => 'VOICE_IN_BOUND_REJECT'))))))),array('$eq' => array('$eventId'=>'release')),'VOICE_IN_BOUND',''),
                   
                    '$cond' => array('$and' => array(array('$eq' => array('$channelType' => 'SMS'))),array('$eq' => array('$requestType' => 'INBOUND')),array(array('$or'=> array(array('$eq' => array('$eventId'=>'SMS_MO'))),array('$eq' => array('$eventId' => null)))),'SMS_MO',''),
                   
                    '$cond' => array('$and' => array(array('$eq' => array('$channelType'=>'VOICE'))),array('$eq' => array('$requestType'=>'OUTBOUND')),array('$eq' => array('$eventId'=>'release')),'VOICE_OUT_BOUND','')
                                        )
                                    )
                                ),'count' => array('$sum' => 1)
                    ),             
            ),
            array('$limit' => 1000)
        ));



Best Regards,
Satya D

Stephen Steneker

unread,
Aug 9, 2014, 9:46:22 PM8/9/14
to mongod...@googlegroups.com
On Saturday, 9 August 2014 20:49:14 UTC+10, Sayta Dommeti wrote:
I am trying to export mongod data into a flat file in a specific format using PHP. To do this, I am using following aggregate query in PHP script. I have basic knowledge to use mongo query in PHP script. But, I got below exception while running PHP application.  Can anyone please help me to resolve this issue ?

From GenReport Cath Exception catched:
Code: 16021Message: Host:27017: exception: the $concat operator does not accept an object as an operand

Hi Sayta,

The $concat expression can only resolve arguments that resolve to strings.

Without an example document it is difficult to troubleshoot .. I would suggest simplifying your query to narrow down the portion that is not working as expected.

Regards,
Stephen

Sayta Dommeti

unread,
Aug 11, 2014, 12:37:59 PM8/11/14
to mongod...@googlegroups.com
Hi Stephen,

Thank you for reply.
 
I have resolved the issue by including another array  within $cancat array as highlighted below.  But, not getting expected result.

Is there any possible way to export the aggregate result into a file other than php script ?

'Service'=> array(
                    '$concat' => array(array(
                    '$cond' => array(array('$and' => array(array('$eq' => array('$channelType' => 'SMS')),array('$eq' => array('$requestType' => 'OUTBOUND')),

                               array('$or' => array(array('$eq' => array('$eventId'=>'SMS_MT'))),array('$eq' => array('$eventId' => 'SMS_MT_PRO'))))),'SMS_MT',''),
                   
                    '$cond' => array(array(
                    '$and' => array(array('$or' => array(array('$eq'=> array('$apiName'=>'VOICE_IN_BOUND')),array('$eq' => array('$apiName' => 'VIRTUAL_NUMBER')),
                    array('$eq' => array('$apiName' => 'VOICE_IN_BOUND_REJECT'))))),array('$eq' => array('$eventId'=>'release'))),'VOICE_IN_BOUND',''),

                    '$cond' => array(array(
                    '$and' => array(array('$eq' => array('$channelType' => 'SMS')),array('$eq' => array('$requestType' => 'INBOUND')),
                    array(array('$or'=> array(array('$eq' => array('$eventId'=>'SMS_MO'))),array('$eq' => array('$eventId' => null)))))),'SMS_MO',''),
                   
                    '$cond' => array(array(
                    '$and' => array(array('$eq' => array('$channelType'=>'VOICE')),array('$eq' => array('$requestType'=>'OUTBOUND')),array('$eq' => array('$eventId'=>'release')))),'VOICE_OUT_BOUND','')
                                        ))
                                    )

Best Regards,
Satya D

Tim Hawkins

unread,
Aug 11, 2014, 1:03:50 PM8/11/14
to mongod...@googlegroups.com

Checkout the $out pipeline operator, that allows you to send the output of the aggregation to a collection, instead of returning it to php.

I think it only works in 2.6.x

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/11af20c7-6ad2-4779-b39d-3fe16adbd67f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sayta Dommeti

unread,
Aug 11, 2014, 2:07:04 PM8/11/14
to mongod...@googlegroups.com
Hi Tim,

Yes, I works in 2.6.x only.  But, We are using MongoDB version is 2.4.3

You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/NdN7FXK4c0s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

For more options, visit https://groups.google.com/d/optout.



--
Regards,
Satya D

Asya Kamsky

unread,
Aug 12, 2014, 6:51:43 PM8/12/14
to mongodb-user
Your problem is that you are using $concat in $group phase to construct your _id and it's not very readable.   
If you pulled the field transformations into $project *before* the group then it would be easier to debug.

Or maybe you can explain what you are trying to achieve in this aggregation and we can see how close your code is to that.

Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages