MongoDB view

Table of contents

scenes to be used

Two views

view behavior

View permissions

View operations


A MongoDB view is a read-only queryable object. The data in the view is obtained through collection queries defined in other collections or views. MongoDB's views are also divided into two types, standard views (referred to as views) and materialized views. The standard view is the view described and used in the general query sense. The data queried in the view is not saved to the database hard disk, but is calculated in real time during the query. In materialized views, data is read directly from disk during query.

scenes to be used

In system development applications, views have a wide range of usage scenarios.

  • To hide some sensitive information, create an employee dataset without personally identifiable information that can be used by applications and developers to query it.
  • Perform calculations on fields in the database to create a sensor dataset with calculated fields and metrics.
  • In order to facilitate statistics and query records, create a view with inventory and order history.

In addition to the above typical application scenarios, views have many application scenarios, providing various conveniences for development and application.

Two views

Two views are introduced at the beginning of the text, standard view and materialized view. There are some similarities and differences between the two views. During the system development process, users can continue to choose two different views according to their actual business needs.

  • Both views return results through collection query aggregation.
  • When querying in standard view, the calculation results are obtained in real time and will not be saved to the hard disk. When querying, you can get the most recently updated data without considering the update of data on disk.
  • The materialized view is stored on the hard disk, and the aggregation pipeline contains the $merge or $out method. When using materialized views, you need to consider data updates, and you may not be able to get the most recently updated data.
  • Standard view queries rely on the index of the collection and cannot create new indexes. System developers can add indexes to materialized views to improve query efficiency.
  • Materialized views are read directly from disk and do not require real-time calculations, so the performance is often inferior to standard views.

view behavior

  • The view is read-only. MongoDB does not allow data to be inserted into the view. When inserting data into the view, mongodb will report an error.
db.placesView.insertOne({ _id: 1, category: "café" })
WriteError({
	"index" : 0,
	"code" : 166,
	"errmsg" : "Namespace test.placesView is a view, not a collection",
	"op" : {
		"_id" : 1,
		"category" : "café"
	}
})
  • In the view, when using the sorting pipeline in the collection operation aggregation, the use of database memory is limited to 100M. When the memory exceeds 100M, {allowDiskUsage:true} needs to be used. When the allowDiskUsage parameter is defined in the query view statement, the allowDiskUse parameter defined in the view will be overwritten.
  • When the collection the view depends on is a sharded collection, the view is also sharded. Cannot use $lookup, $graphLookup operations to obtain sharded views
  • The standard view of a time series collection is writable. MongoDB allows inserting data into standard views of time series collections.

View permissions

  • When creating a view, the user needs to have createCollection permission. At the same time, the user needs to have query permissions for the collection on which the query view depends.

View operations

Mongodb supports the creation, query, modification and deletion of views. Due to space limitations, this article will not introduce it for now. Please refer to subscribing to other articles.

おすすめ

転載: blog.csdn.net/wilsonzane/article/details/135201008