In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one. If we want to return pointer on all documents then use empty() parameter that returns all documents one by one. It takes only some optional parameters. The first optional parameter is the selection criteria on which we want to return a cursor. To return all documents in a collection use empty document({}). Using this method you can also replace embedded documents. You can also use this method in multi-document transactions. If you use this method in the mongo shell, then the shell will automatically iterate the cursor to display up to 20 documents in the collection, if you want to continue then type it or you can manually iterate the result of the find() method by assigning the returned cursor to a variable with the var keyword. You can also modify the behavior of this method using cursor methods. Show
Syntax:
Optional parameters:
This document takes: { field1: <value1>, field2: <value2> ... } Here if the value of the field is 1/true then it specifies the inclusion of the field, or if the value of the field is 0/false then it specifies the exclusion of the field. Return: It returns a cursor to the documents that match the selection criteria. When the find() method returns documents, the method is actually returning a cursor to the documents. Find method in MongoDB Query is used to fetch a particular document from the MongoDB collection. There are totally six methods available in Mongo DB by which we can fetch particular records. Those methods include:
Syntax: find(query,projection) Find method consists of two parameters by which we can fetch a particular record. If we don’t use these two parameters then the find method will return all the documents available within the MongoDB collection. Query – This is an optional parameter which defines the selection criteria. In simple words, what you want to search within a collection will be defined as a query. Projection – This is an optional parameter which defines what to return when the query criteria successfully meet. In simple words, it is a kind of decision making which will take the decision upon the criteria. Find All DocumentsIn order to fetch all the records that are available within a collection, we need to use the find method having an empty parameter. In simple words, we will not use any parameter when we need all the records. Code db.staff().find() Figure 1: In Mongo Shell Figure 2: In Robo 3T Find Specific DocumentsIn order to fetch a specific document from the MongoDB collection, we can use a query parameter in the find method. This parameter will help us to fetch only that record which meets the criteria. In the following example, we are going to fetch the record by staff ID, where the staff ID is equal three and it will return us only that document. Code db.staff().find({staff_id:3}) Figure 3: In Mongo Shell Figure 4: In Robo 3T Find Documents with Specific FieldsIn order to fetch specific fields, we have to use projection within the find method. As discussed earlier, projection is a kind of decision making. It takes the decision to show and hide fields. In the following example, you can observe that we are only getting a record from the staff member name field. Code db.staff.find({},{_id:0,staff_member_name:1}) Figure 5: In Mongo Shell Figure 6: In Robo 3T Find Specific Documents with Conditional CriteriaWe can use conditions to filter a specific record critically. In the above examples, we discussed how to fetch a specific document. But now we will apply a condition on that specific document to return only those documents which successfully meet the condition. In the following example, we are fetching the record by member name “Alex”, where the staff ID is greater than 2. Code db.staff().find({staff_member_name:”Alex”, staff_id:{$gt:2}}) Figure 7: In Mongo Shell Figure 8: In Robo 3T ConclusionFind method is used to fetch a document from the MongoDB collection. Using the Find method, we can fetch specific documents as well as the particular fields that we need. We can also use other find methods to retrieve specific documents as per our requirement. How do I get an entire collection in MongoDB?To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database.
How do I find documents in MongoDB?To find documents that match a set of selection criteria, call find() with the <criteria> parameter. MongoDB provides various query operators to specify the criteria. For a list of the query operators, see Query Selectors.
Where are documents stored in MongoDB?Documents are stored on disk using block compression to reduce storage usage. Documents are automatically uncompressed in memory when retrieved by the MongoDB server. Each collection & index is stored in a separate file within the storage. dbPath for your deployment.
How will you get the data collection from your MongoDB database?Collections are assigned an immutable UUID. The collection UUID remains the same across all members of a replica set and shards in a sharded cluster. To retrieve the UUID for a collection, run either the listCollections command or the db. getCollectionInfos() method.
|