I’m having an issue in my mongo group query: (this is PHP code)
$conditions = array(
'user' => array(
'$ne' => $uid
)
);
$group = $db->words->group(
array("word" => true),
array("count" => 0),
"function(obj, prev) { prev.count += 1 }",
$conditions
);
Imagine the following documents in words collection
{
_id: 3,
word: "hello",
user: "test"
}
{
_id: 2,
word: "world",
user: ""
}
{
_id: 1,
word: "test",
user: ""
}
What I needed to be returned from the group command was:
{
word: "world",
count: 1
}
I need to have only the words that have no user associated in any document. Currently I get:
{
word: "test",
count: 2
}
{
word: "world",
count: 1
}
Does this make sense? I’m still starting with mongo…
Thanks
I do not think you can filter the count directly within the group().
You will need to filter it out client side. I suspect this is prolly what you want anyway since you will wish to sort on count in one way or another (I know I would). So yea you will need to go through each doc taking out the count field.
I believe the aggregation framework will be nicer with this:
http://docs.mongodb.org/manual/reference/aggregation/#_S_group
You could use it as a pipe and then replace the field in another pipe after $sort or whatever.
Hope this helps,