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. Great Article Cloud Computing Projects

    Networking Projects

    Final Year Projects for CSE

    JavaScript Training in Chennai

    JavaScript Training in Chennai

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete