从3.4版本开始,MongoDB还支持从现有的集合或其他视图创建只读视图。

创建view

从3.4开始引入了创建和第一个一个试图

1
2
3
db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline> } ) 
db.runCommand({create:'userView',viewOn:'user',pipeline:[{$match:{uname:'cfl'}}]})
db.createView(<view>, <source>, <pipeline>, <collation> ) //新版本的mongo shell

行为

  1. view 是只读的,不能进行写操作
  2. view 是我索引的集合
  3. view 不能执行 aggregation pipeline 的下面操作:
    1. db.collection.mapReduce()
    2. $text
    3. geoNear 命令和 $geoNear 管道
      4.find()操作视图不支持以下预算符
    4. $
    5. $elemMatch
    6. $slice
    7. $meta
  4. 你不能重命名view

删除一个view

db.collection.drop()