Use createView with allowDiskUse

蹲街弑〆低调 提交于 2020-01-14 10:20:06

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/41149573/use-createview-with-allowdiskuse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!