mongodb - Counting sub-document matches in a mongo document -
i have failry simple structure documents:
{regid: 1, data: {[{val: 123456}, {val: 324234}, {val: 4353453}, .......]}}
the data element array may contain between 30 , 60 sub-documents , collection has ~53000 documents, grow larger.
given array of vals, input, [11563012,11563011,82867218,83866648, ....], want return documents have @ least 3 matching data.val. currently, query using $in modifier , $where clause calls js function (countmatches). $in modifier returns document contains @ least 1 item imput , $where function iterates through each document.date, counting matches in input , returns documents above threshold:
db.foo.find({"data.val": {$in: [11563012,11563011,82867218,83866648,.......]}, $where: "countmatches(this.data, [11563012,11563011,82867218,83866648,......])>=3"}).count();
similar questions (http://groups.google.com/group/mongodb-user/browse_thread/thread/fa291575fd47c010) seem indicate way count matchs in "sub-documents" either js function in $where clause or group() aggregate function.
my question is, there better method counting "hits" in sub-document? semantically similar finding "tagged" documents, i.e. return documents have matching tags of [tag1, tag2, tag3, tag4,.....]
you have use $where
, there no better way this.
be aware if $in
filter returns large portion of collection won't able scale solution... (and it's going tough scale other solution)
Comments
Post a Comment