Group By ... Having count(*) > 1 ... with PHP Driver

440 views
Skip to first unread message

Tream

unread,
Feb 26, 2013, 4:40:57 AM2/26/13
to mongod...@googlegroups.com
Hi guys,
I have many documents like:

{
  "userID" => 1,
  "username" => "Foo"
}
{
  "userID" => 2,
  "username" => "Bar"
}
{
  "userID" => 3,
  "username" => "Foo"
}

Now I want to check, if there are any documents with same username. Is there a way to do this via PHP Driver?

Thank you very much!

Sam Millman

unread,
Feb 26, 2013, 6:30:42 AM2/26/13
to mongod...@googlegroups.com
Something like:

$db->users->aggregate(array(
    array('$group' => array('_id' => 'username', 'u_c' => array('$sum' => 1)),
    array('$match' => array('u_c' => 1))
))



--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tream

unread,
Feb 26, 2013, 7:08:58 AM2/26/13
to mongod...@googlegroups.com
Hi Sammaye,
thank you very much for your post.

Something looks wrong with the PHP-Driver. I think, it would be much
easier to run this query just in shell. I tried to "convert" your code into a MongoDB query:

db.userdata.aggregate({
    {
        $group:{
            {
                '_id':'username',
                'u_c':{$sum:1}
            }
        }
    },
    {
        $match:{
            {
                'u_c':{'$gt':1}
            }
        }
    }   
});

But I see in shell:

SyntaxError: invalid property id (shell):2

I just added "'u_c':{'$gt':1}"
 

Sam Millman

unread,
Feb 26, 2013, 7:19:42 AM2/26/13
to mongod...@googlegroups.com
Whops stupid syntax errors:

db.userdata.aggregate([
    { $group: {'_id': '$username', 'u_c': { $sum:1 } } },
    { $match: { 'u_c' : { $gt:1 } } }
])

That should work



--

Tream

unread,
Feb 26, 2013, 8:09:41 AM2/26/13
to mongod...@googlegroups.com
The problem is:


Fatal error: Call to undefined method MongoCollection::aggregate()

I Don´t have this method :(

Sam Millman

unread,
Feb 26, 2013, 8:11:42 AM2/26/13
to mongod...@googlegroups.com
Yea you need to upgrde the PHP driver or you can use the command method:

$db->command(array(
    'aggregate' => 'userdata',
    'pipeline' => array( // your query )
));


Tream

unread,
Feb 26, 2013, 8:38:55 AM2/26/13
to mongod...@googlegroups.com
Okay, I see. I asked the sysadmin to update (from 1.2.10).


$db->command(array(
    'aggregate' => 'userdata',
    'pipeline' => array( // your query )
));

Now I run:

$users = $db->command(array(

        'aggregate' => 'userdata',
        'pipeline' => array(
            array('$group' => array('_id' => 'username', 'u_c' => array('$sum' => 1))),
            array('$match' => array('u_c' => array('$gt' => 1)))
        )
    ));

    foreach($users as $user){
      print_r($user);   
    }

Shows:


Array
(
    [0] => Array
        (
            [_id] => username
            [u_c] => 269170 // What is the number of all user
        )

)

So how to get the results correctly? There are at least 2 documents with same username. 

Sam Millman

unread,
Feb 26, 2013, 9:52:09 AM2/26/13
to mongod...@googlegroups.com
I made a typo when I wrote it at first, you will notice the console command I gave had a slight change of:


array('$group' => array('_id' => 'username', 'u_c' => array('$sum' => 1))),

to:

array('$group' => array('_id' => '$username', 'u_c' => array('$sum' => 1))),

The $ before username denotes to get the fields value, otherwise it will literally ue "username"


--

Tream

unread,
Feb 26, 2013, 10:26:17 AM2/26/13
to mongod...@googlegroups.com
Yeah, this works just perfect. Sammaye, thank you very much, sir! :)
Reply all
Reply to author
Forward
0 new messages