Projection in MongoDB
In MongoDB, projection's meaning is to select only the necessary data instead of selecting the entire data of a Document. If a Document has 5 fields and you only need 3 fields, you should only select 3 fields from that Document.
The find () method in MongoDB
The find () method in MongoDB, explained in Query Document, accepts the second arbitrary parameter which is a list of the fields you want to retrieve. In MongoDB, when you execute the find () method, it displays all the fields of a Document. To limit this, you need to set a list of fields with a value of 1 or 0. Value 1 is used to display the field, while 0 is used to hide the field.
Syntax
The basic syntax of find () method for projection is as follows:
> db . COLLECTION_NAME . find ({},{ KEY : 1 })
For example
You follow Collection with the name mycol with the following data:
{ "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" }
The following example displays the title of the Document while querying it.
> db . mycol . find ({},{ "title" : 1 , _id : 0 }) { "title" : "MongoDB Overview" } { "title" : "NoSQL Overview" } { "title" : "Tutorials Point Overview" } >
You should note that the _id field is always displayed while executing the find () method, if you don't want this field, then you set it to 0.
According to Tutorialspoint
Previous article: Delete Document in MongoDB
Next article: Limit the record in MongoDB
You should read it
May be interested
- Shard in MongoDBsharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
- Learn about Java Driver in MongoDBin the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
- Install MongoDBinstructions for installing mongodb on windows.
- Learn about 100 inch, 120 inch, 150 inch projection screenshow is the screen size of 100 inch, 120 inch, 150 inch screens? should i choose the most suitable presentation and entertainment?
- Map Reduce in MongoDBin mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
- Aggregation in MongoDBaggregation can be understood as aggregation. the aggregation operation handles data records and returns calculated results. the operations group the values from multiple documents together, and can perform multiple operations on the grouped data to return a single result. in sql, count (*) and group by are equivalent to aggregation in mongodb.
- Data modeling in MongoDBdata in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
- How to use Aggregation Pipeline in MongoDBif you are using mongodb's mapreduce, it is best to switch to aggregation pipeline for more efficient computation.
- GridFS in MongoDBgridfs is the mongodb specification for storing and collecting large files such as images, audio, video files, etc. it is a type of file system to store files but its data is stored inside mongodb collections. .
- Query analysis in MongoDBanalyzing queries is a very important aspect to assess the effectiveness of database and the effectiveness of the designed index. we will explore the two most frequently used queries, $ explain and $ hint.