Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use createView with allowDiskUse

In the mongo aggregation framework is possible to use the option {allowDiskUse:true}. This is really useful when some heavy operations such as sorting, which cannot be performed in memory.

I'm trying to do the same with createView (available in Mongo 3.4), but I cannot find the place where allowDiskUse can be introduced.

In the normal aggregation framework:

db.mydb.aggregate([....,{$sort:{"a":-1}}],{allowDiskUse:true})

works, but:

db.createView("newview","mydb",[....,{$sort:{"a":-1}}],{allowDiskUse:true})

produces the error

The field 'allowDiskUse' is not a valid collection option.

Of course, I can just remove {allowDiskUse:true}. Then the view is created, but when I try:

> db.newview.find()
Error: error: {
    "ok" : 0,
    "errmsg" : "Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.",
    "code" : 16819,
    "codeName" : "Location16819"
}

How to create a view that include large operations?

like image 895
RafaelCaballero Avatar asked Dec 14 '16 18:12

RafaelCaballero


2 Answers

Just in case someone finds the same problem, Kyle Suarez at jira proposed the following workaround:

 db.newview.aggregate([], { allowDiskUse: true });

that is, using an aggregation on the view solves the problem.

like image 122
RafaelCaballero Avatar answered Dec 05 '22 17:12

RafaelCaballero


You can't use allowDiskUse when creating a view because it is not a create option. Actually the only supported option is the collation option also new in version 3.4

One way to get around this to pass allowDiskUse to your aggregation query, use the $out option to write your aggregation result to a new collection and create your view on the newly created collection.

But the view created this way will not have any value because you will need to recreated the new collection with $out every time before querying the view.

Another workaround is to performing an aggregation on the view and pass in the allowDiskUse option as mentioned on this closed JIRA

like image 34
styvane Avatar answered Dec 05 '22 15:12

styvane