<div dir="ltr" style="text-align: left;" trbidi="on">In this tutorial one Of the four basic database operations (i.e. CRUD), read operation are those that retrieve records or documents from a collection in MongoDB. For general information about read operations and the factors that affect their performance, see Read Operations; for documentation of the other CRUD operations.</p>
<p>You can retrieve documents from MongoDB using either of the following methods:</p>
<ul class="simple">
<li><i>find</i></li>
<li><i>findOne</i></li>
</ul>
<h2>Find</h2>
<p>The find() method is the primary method to select documents from a collection. The find() method returns a cursor that contains a number of documents. Most <i>drivers</i> provide application developers with a native iterable interface for handling cursors and accessing documents. The find() method has the following syntax:</p>
<div class="highlight-javascript">
<div class="highlight">
<pre>db.collection.find( <;query>;, <;projection>; ) ;</pre>
<pre> ;</pre>
</div>
</div>
<div class="admonition-corresponding-operation-in-sql admonition">
<div class="first admonition-title">Corresponding Operation in SQL</div>
<p>The find() method is analogous to the SELECT statement, while:</p>
<ul class="last simple">
<li>the <;query>; argument corresponds to the WHERE statement, and</li>
<li>the <;projection>; argument corresponds to the list of fields to select from the result set.</li>
</ul>
</div>
<h2>Find One</h2>
<p>The findOne() method selects and returns a single document from a collection and returns that document. findOne() does <i>not</i> return a cursor.</p>
<p>The findOne() method has the following syntax:</p>
<div class="highlight-javascript">
<div class="highlight">
<pre>db.collection.findOne( <;query>;, <;projection>; ) ;</pre>
<pre> ;</pre>
</div>
</div>
<p>Except for the return value, findOne() method is quite similar to the find() method; in fact, internally, the findOne() method is the find() method with a limit of 1.</p>
<h3>Cursor</h3>
<p>The find() method returns a <i class="xref std std-term">cursor</i> to the results; however, in the mongo shell, if the returned cursor is not assigned to a variable, then the cursor is automatically iterated up to 20 times ; to print up to the first 20 documents that match the query, as in the following example:</p>
<div class="highlight-javascript">
<div class="highlight">
<pre>db.bios.find( { _id: 1 } );
</pre>
</div>
</div>
<div class="highlight-javascript"> ;<b><a href="http://docs.mongodb.org/manual/applications/read/" target="_blank">Click here</a> to know more about the Read Data and its options.</b></div>
<p>
In Java MongoDB API, you can use collection.find() to get / query document from collection. Here is few common use cases :</div>
<p>
For this example we have insert some document into the mongoDB &#8220;<b>dineshonjavaDB</b>&#8220;<br />
as follows inserting 10 documents to the &#8220;<b>employee</b>&#8221; collection on mongoDB.</p>
<pre class="java" name="code">for (int i=10001; i <;= 10010; i++) {
 collection.insert(new BasicDBObject().append("empId", i));
}
</pre>
<p><b>1. Fetch first record. </b></p>
<pre class="java" name="code">DBObject doc = collection.findOne(); //get first document
System.out.println(dbObject);
</pre>
<p><b>2. Get the set of document. </b></p>
<pre class="java" name="code">DBCursor cursor = collection.find();
while(cursor.hasNext()) {
 System.out.println(cursor.next());
}
</pre>
<p><b>3. fetch documents where empId = 8. </b></p>
<pre class="java" name="code">BasicDBObject query = new BasicDBObject();
query.put("empId", 10008);
DBCursor cursor = collection.find(query);
while(cursor.hasNext()) {
 System.out.println(cursor.next());
}
</pre>
<p><b>4. fetch documents where number = 10009 and 10010, use &#8220;$in&#8221; operator. </b></p>
<pre class="java" name="code">BasicDBObject query = new BasicDBObject();
List<;Integer>; list = new ArrayList<;Integer>;();
list.add(10009);
list.add(10010);
query.put("empId", new BasicDBObject("$in", list));
DBCursor cursor = collection.find(query);
while(cursor.hasNext()) {
 System.out.println(cursor.next());
}
</pre>
<p><b>5. fetch documents where empId >; 10003, use &#8220;$gt&#8221; operator. </b></p>
<pre class="java" name="code">BasicDBObject query = new BasicDBObject();
query.put("empId", new BasicDBObject("$gt", 10003));
DBCursor cursor = collection.find(query);
while(cursor.hasNext()) {
 System.out.println(cursor.next());
}
</pre>
<p><b>6. fetch documents where 10002 <; number <; 10005, combine two operators, &#8220;$gt&#8221; and &#8220;$lt&#8221;. </b></p>
<pre class="java" name="code">BasicDBObject query = new BasicDBObject();
query.put("empId", new BasicDBObject("$gt", 10002).append("$lt", 10005));
DBCursor cursor = collection.find(query);
while(cursor.hasNext()) {
 System.out.println(cursor.next());
}
</pre>
<p>
<b>7. fetch documents where number != 10001, use &#8220;$ne&#8221; operator.</b></p>
<pre class="java" name="code">BasicDBObject query = new BasicDBObject();
query.put("empId", new BasicDBObject("$ne", 10001));
DBCursor cursor = collection.find(query);
while(cursor.hasNext()) {
 System.out.println(cursor.next());
}
</pre>
<p>
Now lets see the full example and run this ..</p>
<pre class="java" name="code">package com.dineshonjava.mongo.test;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * @author Dinesh Rajput
 *
 */
public class ReadDocumentDemo {

 public static void main(String[] args) {
 
 try {
 
 Mongo mongo = new Mongo("localhost", 27017);
 DB db = mongo.getDB("dineshonjavaDB");
 
 // get a single collection
 DBCollection collection = db.getCollection("employeesCollection");

 // insert empId 10001 to 10010 for testing
 for (int i = 10001; i <;= 10010; i++) {
 collection.insert(new BasicDBObject().append("empId", i));
 }

 // get first document
 DBObject dbObject = collection.findOne();
 System.out.println(dbObject);
 System.out.println("*****************First Test*****************");
 // get all available documents
 DBCursor cursor = collection.find();
 while (cursor.hasNext()) {
 System.out.println(cursor.next());
 }
 System.out.println("*****************Second Test*****************");
 // get document, where empId = 10003
 BasicDBObject query = new BasicDBObject();
 query.put("empId", 10003);
 DBCursor cursor2 = collection.find(query);
 while (cursor2.hasNext()) {
 System.out.println(cursor2.next());
 }
 System.out.println("*****************Third Test*****************");
 // get document, where empId = 10002 and empId = 10004
 BasicDBObject query2 = new BasicDBObject();
 List<;Integer>; list = new ArrayList<;Integer>;();
 list.add(10002);
 list.add(10004);
 query2.put("empId", new BasicDBObject("$in", list));
 DBCursor cursor3 = collection.find(query2);
 while (cursor3.hasNext()) {
 System.out.println(cursor3.next());
 }
 System.out.println("*****************Fourth Test*****************");
 // get document, where empId >; 10006
 BasicDBObject query3 = new BasicDBObject();
 query3.put("empId", new BasicDBObject("$gt", 10006));
 DBCursor cursor4 = collection.find(query3);
 while (cursor4.hasNext()) {
 System.out.println(cursor4.next());
 }
 System.out.println("*****************Fifth Test*****************");
 // get document, where 10005 <; empId <; 10007
 BasicDBObject query4 = new BasicDBObject();
 query4.put("empId", new BasicDBObject("$gt", 10005).append("$lt", 10007));
 DBCursor cursor5 = collection.find(query4);
 while (cursor5.hasNext()) {
 System.out.println(cursor5.next());
 }
 System.out.println("*****************Sixth Test*****************");
 // get document, where empId != 10008
 BasicDBObject query5 = new BasicDBObject();
 query5.put("empId", new BasicDBObject("$ne", 10008));
 DBCursor cursor6 = collection.find(query5);
 while (cursor6.hasNext()) {
 System.out.println(cursor6.next());
 }

 System.out.println("Done");
 
 } catch (UnknownHostException e) {
 e.printStackTrace();
 } catch (MongoException e) {
 e.printStackTrace();
 }
 
 }

}
</pre>
<p>If every thing is fine then run as Java Application we will get the following output on the console. </p>
<div style="background-color: #ff99cc;"><b>output: ;</b><br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5da&#8221;} , &#8220;empId&#8221; : 10001}<br />
 ;*****************First Test*****************<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5da&#8221;} , &#8220;empId&#8221; : 10001}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5db&#8221;} , &#8220;empId&#8221; : 10002}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5dc&#8221;} , &#8220;empId&#8221; : 10003}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5dd&#8221;} , &#8220;empId&#8221; : 10004}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5de&#8221;} , &#8220;empId&#8221; : 10005}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5df&#8221;} , &#8220;empId&#8221; : 10006}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e0&#8221;} , &#8220;empId&#8221; : 10007}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e1&#8221;} , &#8220;empId&#8221; : 10008}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e2&#8221;} , &#8220;empId&#8221; : 10009}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e3&#8221;} , &#8220;empId&#8221; : 10010}<br />
 ;*****************Second Test*****************<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5dc&#8221;} , &#8220;empId&#8221; : 10003}<br />
*****************Third Test*****************<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5db&#8221;} , &#8220;empId&#8221; : 10002}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5dd&#8221;} , &#8220;empId&#8221; : 10004}<br />
 ;*****************Fourth Test*****************<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e0&#8221;} , &#8220;empId&#8221; : 10007}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e1&#8221;} , &#8220;empId&#8221; : 10008}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e2&#8221;} , &#8220;empId&#8221; : 10009}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e3&#8221;} , &#8220;empId&#8221; : 10010}<br />
*****************Fifth Test*****************<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5df&#8221;} , &#8220;empId&#8221; : 10006}<br />
 ;*****************Sixth Test*****************<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5da&#8221;} , &#8220;empId&#8221; : 10001}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5db&#8221;} , &#8220;empId&#8221; : 10002}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5dc&#8221;} , &#8220;empId&#8221; : 10003}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5dd&#8221;} , &#8220;empId&#8221; : 10004}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5de&#8221;} , &#8220;empId&#8221; : 10005}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5df&#8221;} , &#8220;empId&#8221; : 10006}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e0&#8221;} , &#8220;empId&#8221; : 10007}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e2&#8221;} , &#8220;empId&#8221; : 10009}<br />
 ;{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5106c015c781b5829079a5e3&#8221;} , &#8220;empId&#8221; : 10010}<br />
Done </div>
<p>
<b>Download Source Code + Libs</b><br />
<a href="https://sites.google.com/site/dinesh9582486434/my-forms/MongoDBQueryDemo.zip?attredirects=0&;d=1" target="_blank"><b>MongoDBQueryDemo.zip</b></a></p>
<p><b>Reference</b></p>
<p><a href="http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ConditionalOperators" target="_blank">MongoDB operator documentation </a></p>
<div style="background-color: #ff99cc;"> ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;<b> ; ; ; ; <;<;<a href="https://dineshonjava.com/2013/01/java-mongodb-updating-document-to.html">previous</a><;<; ; ; ; ; ; ; ; ; ; ; ; ; || <a href="https://dineshonjava.com/2013/01/introduction-to-mongodb.html">index ; </a>||  ;  ;  ;  ; >;>;<a href="https://dineshonjava.com/2013/01/java-mongodb-convert-json-data-to.html">next</a>>;>;</b></div>
<p></p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/java-mongodb-updating-document-to/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/java-mongodb-convert-json-data-to/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '588'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…