Is it possible to use "$ where" in the mongodb aggregation function

I need to get the length of a string value in MongoDB using aggregation functions.

he works in

db.collection_name.find({"$where":"this.app_name.length===12"}) 

but when implanted in

 db.collection_name.aggregate({$match: {"$where":"this.app_name.length===12"} }, { $group : { _id : 1, app_downloads : {$sum: "$app_downloads"} } } ); 

I got this result:

 failed: exception: $where is not allowed inside of a $match aggregation expression 

The question is, can $ where be used in aggregation functions? or is there a way to get the length of a string value in an aggregation function?

Thanks in advance Eric

+4
source share
2 answers

MongoDB does not support $ where in the aggregation pipeline and hopes this will never happen because JavaScript slows down. However, you still have options:

1) Subordinate an additional field (for example, app_name_len), than save the length of app_name and request it when necessary.

2) You can try the extremely slow MapReduce environment where you can write clusters using JavaScript.

+3
source

Today I had the same problem.

Mongodb does not support this.app_name.length, but you can make this condition with $ regex - it is not very fast, but it still works.

 {"app_name": { $regex: /^.{12}$/ }} 
0
source

Source: https://habr.com/ru/post/1487918/


All Articles