Actually a couple of additional comments.
You cannot pass {allowDiskUse:any}
to view definitions. You can see some of the rationale here: https://jira.mongodb.org/browse/SERVER-27440
However, if you want to dump out a command which will create the view, you can do it like this (warning, no guarantees that this will continue to work in the future):
db.system.views.find()
{ "_id" : "demo.v1", "viewOn" : "people", "pipeline" : [ { "$project" : { "_id" : 0 } } ] }
{ "_id" : "demo.v2", "viewOn" : "people", "pipeline" : [ { "$match" : { "a" : { "$ne" : 1 } } } ], "collation" : { "locale" : "fr", "caseLevel" : false, "caseFirst" : "off", "strength" : 1, "numericOrdering" : false, "alternate" : "non-ignorable", "maxVariable" : "punct", "normalization" : false, "backwards" : false, "version" : "57.1" } }
db.system.views.aggregate([{$replaceWith:{
create:{$substr:["$_id", {$add:[1,{$indexOfCP:["$_id","."]}]},999]},
viewOn: "$viewOn", pipeline:"$pipeline", collation:"$collation"
}}])
{ "create" : "v1", "viewOn" : "people", "pipeline" : [ { "$project" : { "_id" : 0 } } ] }
{ "create" : "v2", "viewOn" : "people", "pipeline" : [ { "$match" : { "a" : { "$ne" : 1 } } } ], "collation" : { "locale" : "fr", "caseLevel" : false, "caseFirst" : "off", "strength" : 1, "numericOrdering" : false, "alternate" : "non-ignorable", "maxVariable" : "punct", "normalization" : false, "backwards" : false, "version" : "57.1" } }
Here are two ways to get the data about views from the system.views collection. Note that the second version is the exact document you would pass to db.runCommand()
to create the view.
P.S. I’m running 4.2 where $replaceWith
is an alias for $replaceRoot:{newRoot:
(slightly shorter to write)