Docs Menu

Docs HomeMongoDB Manual

Modify a View

On this page

  • Example
  • Drop and Recreate the View
  • Use the collMod Command

To modify a view, you can either:

  • Drop and recreate the view.

  • Use the collMod command.

Consider the following view named lowStock:

db.createView(
"lowStock",
"products",
[ { $match: { quantity: { $lte: 20 } } } ]
)

The following commands modify lowStock by dropping and recreating the view:

db.lowStock.drop()
db.createView(
"lowStock",
"products",
[ { $match: { quantity: { $lte: 10 } } } ]
)

Alternatively, you can use the collMod command to modify the view:

db.runCommand( {
collMod: "lowStock",
viewOn: "products",
"pipeline": [ { $match: { quantity: { $lte: 10 } } } ]
} )
←  Create a View with Default CollationRemove a View →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.