Reputation: 9685
I'm doing a fuzzy match to an input sentence, and I currently have a step in the AF like this:
{ "$group" : { "_id" : "$_id" , "score" : { "$sum" : 1}}}
but I'd like to be able to score shorter matches higher and want to do something like:
{ "$group" : { "_id" : "$_id" , "score" : { "$sum" : "1 / $length"}}}
Is something like this possible?
Upvotes: 0
Views: 35
Reputation: 505
I guess you want something like this. the default "_id" values are unique, so probably you want to group some other parameter. So I used another parameter idd, instead of _id here.
> db.tmp1.aggregate({'$group':{'_id':'$idd', 'count':{'$sum':1} }},{ $project : { _id: 1, suminv :{$divide:[1, '$count'] } } } );
{ "_id" : 2, "suminv" : 0.3333333333333333 }{ "_id" : 1, "suminv" : 0.5 }
> db.tmp1.find();
{ "_id" : ObjectId("572a5a74024dc1f2fe4b432b"), "idd" : 1, "score" : 2 }
{ "_id" : ObjectId("572a5a79024dc1f2fe4b432c"), "idd" : 1, "score" : 1 }
{ "_id" : ObjectId("572a5a82024dc1f2fe4b432d"), "idd" : 2, "score" : 1 }
{ "_id" : ObjectId("572a5a86024dc1f2fe4b432e"), "idd" : 2, "score" : 2 }
{ "_id" : ObjectId("572a5a8e024dc1f2fe4b432f"), "idd" : 2, "score" : 3 }
Upvotes: 0
Reputation: 1168
Yes, it should be possible (assuming $length is a field name in your documents), but the command should look like this:
{ "$group" : { "_id" : "$_id" , "score" : { $sum : {$divide: [1, "$length"]}}}}
You can find more details about possible math expressions on this page.
Upvotes: 1