0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

fix group with [] in inital SERVER-361

This commit is contained in:
Eliot Horowitz 2009-10-15 21:19:24 -04:00
parent 5696d65d83
commit f22a821979
3 changed files with 52 additions and 3 deletions

View File

@ -1278,7 +1278,7 @@ namespace mongo {
" if ( $arr[n] == null ){ "
" next = {}; "
" Object.extend( next , $key ); "
" Object.extend( next , $initial ); "
" Object.extend( next , $initial , true ); "
" $arr[n] = next; "
" next = null; "
" } "

45
jstests/group4.js Normal file
View File

@ -0,0 +1,45 @@
t = db.group4
t.drop();
function test( c , n ){
var x = {};
c.forEach(
function(z){
assert.eq( z.count , z.values.length , n + "\t" + tojson( z ) );
}
);
}
t.insert({name:'bob',foo:1})
t.insert({name:'bob',foo:2})
t.insert({name:'alice',foo:1})
t.insert({name:'alice',foo:3})
t.insert({name:'fred',foo:3})
t.insert({name:'fred',foo:4})
x = t.group(
{
key: {foo:1},
initial: {count:0,values:[]},
reduce: function (obj, prev){
prev.count++
prev.values.push(obj.name)
}
}
);
test( x , "A" );
x = t.group(
{
key: {foo:1},
initial: {count:0},
reduce: function (obj, prev){
if (!prev.values) {prev.values = [];}
prev.count++;
prev.values.push(obj.name);
}
}
);
test( x , "B" );

View File

@ -125,9 +125,13 @@ assert.gt = function( a , b , msg ){
doassert( a + " is not greater than " + b + " : " + msg );
}
Object.extend = function( dst , src ){
Object.extend = function( dst , src , deep ){
for ( var k in src ){
dst[k] = src[k];
var v = src[k];
if ( deep && typeof(v) == "object" ){
v = Object.extend( typeof ( v.length ) == "number" ? [] : {} , v , true );
}
dst[k] = v;
}
return dst;
}