2012-09-29

Morphia and semicolons

I noticed a strange behaviour from Morphia when writing MongoDb group by equivalents. It doesn't seem to like semicolons in your queries. Please see the following example.
db.MongoDbSong.group(
    {key: {artistName: 1, albumName: 1},
    initial: {totalPlays: 0},
    reduce: function(obj, prev) { for(var i=0; i < obj.statsList.length; i++) { if(obj.statsList[i].libraryUuid == 'bf25ff51-da26-4c76-82dc-ada0e4a68c18'){ prev.totalPlays += obj.statsList[i].playCount}}}
})
This code is valid and runs fine inside MongoDb console. Not so in Morphia, it returns a null result.

Workaround

I had to change my query into this, so essentially iterate over the array without using semicolons:
db.MongoDbSong.group(" +
    {key: {artistName: 1, albumName: 1},
    initial: {totalPlays: 0},
    reduce: function(obj, prev) {for each (var item in obj.statsList) if(item.libraryUuid == 'bf25ff51-da26-4c76-82dc-ada0e4a68c18'){ prev.totalPlays += item.playCount}}" +
 "});
It could be that I'm wrong. Anyway, this works and I have submitted this as a defect.

See Scala code using this.

1 comment:

  1. I am really enjoying reading your well written articles. I think you spend numerous effort and time updating your blogabogado dui mecklenburg virginia

    ReplyDelete