Spring Core

Spring Data MongoDB : Query document

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">In this tutorial we will discuss about the querying the document to the mongoDB&period; Document is same as a row in the table of relational database&period; In the our example we will fire query ti access a document from &&num;8220&semi;<b>dojCollection<&sol;b>&&num;8221&semi; of &&num;8220&semi;<b>dineshonjavaDB<&sol;b>&&num;8220&semi;&period;<b>Querying Documents-<&sol;b><&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>You can express your queries using the <i><b>Query <&sol;b><&sol;i>and <b><i>Criteria <&sol;i><&sol;b>classes which have method names that mirror the native <b>MongoDB <&sol;b>operator names such as <b>lt&comma; lte&comma; is&comma; and others<&sol;b>&period; The <i><b>Query <&sol;b><&sol;i>and <i><b>Criteria <&sol;b><&sol;i>classes follow a fluent API style so that you can easily chain together multiple method criteria and queries while having easy to understand code&period; Static imports in Java are used to help remove the need to see the &&num;8216&semi;new&&num;8217&semi; keyword for creating <i><b>Query <&sol;b><&sol;i>and <b><i>Criteria <&sol;i><&sol;b>instances so as to improve readability&period;<&sol;p>&NewLine;<h3 style&equals;"text-align&colon; left&semi;"><b>1&period; Querying documents in a collection-<&sol;b><&sol;h3>&NewLine;<p>We saw how to retrieve a single document using the <i><b>findOne <&sol;b><&sol;i>and <i><b>findById <&sol;b><&sol;i>methods on <b>MongoTemplate <&sol;b>in previous sections which return a single domain object&period; We can also query for a collection of documents to be returned as a list of domain objects&period; Assuming that we have a number of Person objects with name and age stored as documents in a collection and that each person has an embedded account document with a balance&period; We can now run a query using the following code&period;<&sol;p>&NewLine;<p><b>For Example-<&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">import static org&period;springframework&period;data&period;mongodb&period;core&period;query&period;Criteria&period;where&semi; &NewLine;import static org&period;springframework&period;data&period;mongodb&period;core&period;query&period;Query&period;query&semi; &NewLine; &period;&period;&period; &NewLine;List&lt&semi;Employee&gt&semi; result &equals; &NewLine;mongoTemplate&period;find&lpar;query&lpar;where&lpar;"age"&rpar;&period;lt&lpar;50&rpar;&period;and&lpar;"salary"&rpar;&period;gt&lpar;50000&rpar;&rpar; &NewLine;&comma;Employee&period;class&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>All find methods take a Query object as a parameter&period; This object defines the criteria and options used to perform the query&period; The criteria is specified using a Criteria object that has a static factory method named where used to instantiate a new Criteria object&period; We recommend using a static import for <i><b>org&period;springframework&period;data&period;mongodb&period;core&period;query&period;Criteria&period;where<&sol;b><&sol;i> and <b><i>Query&period;query<&sol;i><&sol;b> to make the query more readable&period;<&sol;p>&NewLine;<p>This query should return a list of Employee objects that meet the specified criteria&period; The Criteria class has the following methods that correspond to the operators provided in MongoDB&period;<&sol;p>&NewLine;<p>As you can see most methods return the Criteria object to provide a fluent style for the API&period;<&sol;p>&NewLine;<h3 class&equals;"title">Methods for the Criteria class<&sol;h3>&NewLine;<div class&equals;"itemizedlist">&NewLine;<ul >&NewLine;<li>Criteria <b>all <&sol;b> &lpar;Object o&rpar;Creates a criterion using the &dollar;all operator<&sol;li>&NewLine;<li>Criteria <b>and <&sol;b> &lpar;String key&rpar; Adds a chained Criteria with the specified key to the current Criteria and retuns the newly created one<&sol;li>&NewLine;<li>Criteria <b>andOperator <&sol;b> &lpar;Criteria&&num;8230&semi; criteria&rpar;Creates an and query using the &dollar;and operator for all of the provided criteria &lpar;requires MongoDB 2&period;0 or later&rpar;<&sol;li>&NewLine;<li>Criteria <b>elemMatch <&sol;b> &lpar;Criteria c&rpar; Creates a criterion using the &dollar;elemMatch operator<&sol;li>&NewLine;<li>Criteria <b>exists <&sol;b> &lpar;boolean b&rpar; Creates a criterion using the &dollar;exists operator<&sol;li>&NewLine;<li>Criteria <b>gt <&sol;b> &lpar;Object o&rpar;Creates a criterion using the &dollar;gt operator<&sol;li>&NewLine;<li>Criteria <b>gte <&sol;b> &lpar;Object o&rpar;Creates a criterion using the &dollar;gte operator<&sol;li>&NewLine;<li>Criteria <b>in <&sol;b> &lpar;Object&&num;8230&semi; o&rpar; Creates a criterion using the &dollar;in operator for a varargs argument&period;<&sol;li>&NewLine;<li>Criteria <b>in <&sol;b> &lpar;Collection&lt&semi;&quest;&gt&semi; collection&rpar; Creates a criterion using the &dollar;in operator using a collection<&sol;li>&NewLine;<li>Criteria <b>is <&sol;b> &lpar;Object o&rpar;Creates a criterion using the &dollar;is operator<&sol;li>&NewLine;<li>Criteria <b>lt <&sol;b> &lpar;Object o&rpar;Creates a criterion using the &dollar;lt operator<&sol;li>&NewLine;<li>Criteria <b>lte <&sol;b> &lpar;Object o&rpar;Creates a criterion using the &dollar;lte operator<&sol;li>&NewLine;<li>Criteria <b>mod <&sol;b> &lpar;Number value&comma; Number remainder&rpar;Creates a criterion using the &dollar;mod operator<&sol;li>&NewLine;<li>Criteria <b>ne <&sol;b> &lpar;Object o&rpar;Creates a criterion using the &dollar;ne operator<&sol;li>&NewLine;<li>Criteria <b>nin <&sol;b> &lpar;Object&&num;8230&semi; o&rpar; Creates a criterion using the &dollar;nin operator<&sol;li>&NewLine;<li>Criteria <b>norOperator <&sol;b> &lpar;Criteria&&num;8230&semi; criteria&rpar;Creates an nor query using the &dollar;nor operator for all of the provided criteria<&sol;li>&NewLine;<li>Criteria <b>not <&sol;b> &lpar;&rpar;Creates a criterion using the &dollar;not meta operator which affects the clause directly following<&sol;li>&NewLine;<li>Criteria <b>orOperator <&sol;b> &lpar;Criteria&&num;8230&semi; criteria&rpar;Creates an or query using the &dollar;or operator for all of the provided criteria<&sol;li>&NewLine;<li>Criteria <b>regex <&sol;b> &lpar;String re&rpar; Creates a criterion using a &dollar;regex<&sol;li>&NewLine;<li>Criteria <b>size <&sol;b> &lpar;int s&rpar;Creates a criterion using the &dollar;size operator<&sol;li>&NewLine;<li>Criteria <b>type <&sol;b> &lpar;int t&rpar;Creates a criterion using the &dollar;type operator<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<h3 class&equals;"title">Methods for the Query class<&sol;h3>&NewLine;<div class&equals;"itemizedlist">&NewLine;<ul >&NewLine;<li>Query <b>addCriteria <&sol;b> &lpar;Criteria criteria&rpar; used to add additional criteria to the query<&sol;li>&NewLine;<li>Field <b>fields <&sol;b> &lpar;&rpar; used to define fields to be included in the query results<&sol;li>&NewLine;<li>Query <b>limit <&sol;b> &lpar;int limit&rpar; used to limit the size of the returned results to the provided limit &lpar;used for paging&rpar;<&sol;li>&NewLine;<li>Query <b>skip <&sol;b> &lpar;int skip&rpar; used to skip the provided number of documents in the results &lpar;used for paging&rpar;<&sol;li>&NewLine;<li>Sort <b>sort <&sol;b> &lpar;&rpar; used to provide sort definition for the results<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3 style&equals;"text-align&colon; left&semi;"><b>2&period; Methods for querying for documents-<&sol;b><&sol;h3>&NewLine;<p>The query methods need to specify the target type T that will be returned and they are also overloaded with an explicit collection name for queries that should operate on a collection other than the one indicated by the return type&period;<&sol;p>&NewLine;<div class&equals;"itemizedlist">&NewLine;<ul >&NewLine;<li><b>findAll <&sol;b> Query for a list of objects of type T from the collection&period;<&sol;li>&NewLine;<li><b>findOne<&sol;b> Map the results of an ad-hoc query on the collection to a single instance of an object of the specified type&period;<&sol;li>&NewLine;<li><b>findById<&sol;b> Return an object of the given id and target class&period;<&sol;li>&NewLine;<li><b>find<&sol;b> Map the results of an ad-hoc query on the collection to a List of the specified type&period;<&sol;li>&NewLine;<li><b>findAndRemove<&sol;b> Map the results of an ad-hoc query on the collection to a single instance of an object of the specified type&period; The first document that matches the query is returned and also removed from the collection in the database&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>In Spring data for <b>MongoDB<&sol;b>&comma; you can use<b><i> findOne&lpar;&rpar;&comma; find&lpar;&rpar; and getCollection&lpar;&rpar; <&sol;i><&sol;b>to get &sol; query documents from mongoDB&period; See some of the common ways &colon;<&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">Employee employee &equals; new Employee&lpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol;get first found record&comma; from "dojCollection" collection&comma; where empId &equals; 10001 &NewLine; Employee employee &equals; mongoOperation&period;findOne&lpar;new Query&lpar;Criteria &NewLine; &period;where&lpar;"empId"&rpar;&period;is&lpar;10001&rpar;&rpar;&comma;"dojCollection"&comma; Employee&period;class&rpar;&semi; &NewLine; &NewLine; &sol;&sol;get all found records&comma; from "dojCollection" collection&comma; where empId &lt&semi;&equals; 10001 abs empAge &equals; 21 &NewLine; List&lt&semi;Employee&gt&semi; employees &equals; mongoOperation&period;find&lpar;new Query&lpar;Criteria &NewLine; &period;where&lpar;"empId"&rpar;&period;lte&lpar;10001&rpar;&period;and&lpar;"empAge"&rpar;&period;is&lpar;21&rpar;&rpar;&comma; "dojCollection"&comma; Employee&period;class&rpar;&semi; &NewLine; &NewLine; &sol;&sol;get all records from "dojCollection" collection &NewLine; List&lt&semi;Employee&gt&semi; employees &equals; mongoOperation&period;getCollection&lpar;"dojCollection"&comma; Employee&period;class&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>See the full example to query a document from the &&num;8220&semi;<b>dojCollection<&sol;b>&&num;8221&semi; of the &&num;8220&semi;<b>dineshonjavaDB<&sol;b>&&num;8221&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;02&sol;springquery&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h3><b>Step 1&colon; Creating the domain class Employee<&sol;b><br &sol;>&NewLine;<b>Employee&period;java  <&sol;b><&sol;h3>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;mongo&period;dto&semi; &NewLine; &NewLine;import org&period;springframework&period;data&period;annotation&period;Id&semi; &NewLine;import org&period;springframework&period;data&period;mongodb&period;core&period;mapping&period;Document&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Document &NewLine;public class Employee &lbrace; &NewLine; &commat;Id &NewLine; private int empId&semi; &NewLine; private String empName&semi; &NewLine; private long salary&semi; &NewLine; private int empAge&semi; &NewLine; public int getEmpId&lpar;&rpar; &lbrace; &NewLine; return empId&semi; &NewLine; &rcub; &NewLine; public void setEmpId&lpar;int empId&rpar; &lbrace; &NewLine; this&period;empId &equals; empId&semi; &NewLine; &rcub; &NewLine; public String getEmpName&lpar;&rpar; &lbrace; &NewLine; return empName&semi; &NewLine; &rcub; &NewLine; public void setEmpName&lpar;String empName&rpar; &lbrace; &NewLine; this&period;empName &equals; empName&semi; &NewLine; &rcub; &NewLine; public long getSalary&lpar;&rpar; &lbrace; &NewLine; return salary&semi; &NewLine; &rcub; &NewLine; public void setSalary&lpar;long salary&rpar; &lbrace; &NewLine; this&period;salary &equals; salary&semi; &NewLine; &rcub; &NewLine; public int getEmpAge&lpar;&rpar; &lbrace; &NewLine; return empAge&semi; &NewLine; &rcub; &NewLine; public void setEmpAge&lpar;int empAge&rpar; &lbrace; &NewLine; this&period;empAge &equals; empAge&semi; &NewLine; &rcub; &NewLine; &commat;Override &NewLine; public String toString&lpar;&rpar; &lbrace; &NewLine; return "Employee &lbrack;age&equals;" &plus; empAge &plus; "&comma; empName&equals;" &plus; empName &plus; "&comma; empId&equals;" &NewLine; &plus; empId &plus; "&comma; salary&equals;" &plus; salary &plus; "&rsqb;"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>Step 2&colon; Creating configuration file<br &sol;>&NewLine;mongo-config&period;xml<&sol;b><&sol;h3>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">&lt&semi;beans xmlns&colon;context&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context" xmlns&colon;mongo&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;data&sol;mongo" xmlns&colon;p&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;p" xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" xsi&colon;schemalocation&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans-3&period;0&period;xsd &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;data&sol;mongo &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;data&sol;mongo&sol;spring-mongo-1&period;0&period;xsd"&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- Default bean name is 'mongo' --&gt&semi; &NewLine; &lt&semi;mongo&colon;mongo host&equals;"localhost" port&equals;"27017"&sol;&gt&semi; &NewLine; &lt&semi;&excl;-- Default bean name is 'mongo' --&gt&semi; &NewLine; &lt&semi;mongo&colon;mongo&gt&semi; &NewLine; &lt&semi;mongo&colon;options connections-per-host&equals;"100" &NewLine; threads-allowed-to-block-for-connection-multiplier&equals;"5" &NewLine; max-wait-time&equals;"120000000" &NewLine; connect-timeout&equals;"10000000" &NewLine; socket-keep-alive&equals;"true" &NewLine; socket-timeout&equals;"15000000" &NewLine; auto-connect-retry&equals;"true"&sol;&gt&semi; &NewLine; &lt&semi;context&colon;annotation-config&sol;&gt&semi; &NewLine; &NewLine; &lt&semi;context&colon;component-scan base-package&equals;"com&period;dineshonjava&period;mongo"&gt&semi; &NewLine; &lt&semi;context&colon;exclude-filter type&equals;"annotation" expression&equals;"org&period;springframework&period;context&period;annotation&period;Configuration"&sol;&gt&semi; &NewLine; &lt&semi;&sol;context&colon;component-scan&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes&period; --&gt&semi; &NewLine; &lt&semi;bean class&equals;"org&period;springframework&period;data&period;mongodb&period;core&period;MongoTemplate" id&equals;"mongoTemplate"&gt&semi; &NewLine; &lt&semi;constructor-arg ref&equals;"mongo"&sol;&gt&semi; &NewLine; &lt&semi;constructor-arg name&equals;"databaseName" value&equals;"dineshonjavaDB"&sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h3><b>Step 3&colon; Creating Class which inserting the document into the mongoDB&period;<&sol;b><br &sol;>&NewLine;<b>HelloMongoDB&period;java<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;mongo&period;main&semi; &NewLine; &NewLine;import static org&period;springframework&period;data&period;mongodb&period;core&period;query&period;Criteria&period;where&semi; &NewLine;import static org&period;springframework&period;data&period;mongodb&period;core&period;query&period;Query&period;query&semi; &NewLine; &NewLine;import java&period;util&period;ArrayList&semi; &NewLine;import java&period;util&period;List&semi; &NewLine; &NewLine;import org&period;springframework&period;beans&period;factory&period;annotation&period;Autowired&semi; &NewLine;import org&period;springframework&period;data&period;mongodb&period;core&period;MongoOperations&semi; &NewLine;import org&period;springframework&period;data&period;mongodb&period;core&period;query&period;Update&semi; &NewLine;import org&period;springframework&period;stereotype&period;Repository&semi; &NewLine; &NewLine;import com&period;dineshonjava&period;mongo&period;dto&period;Employee&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Repository &NewLine;public class HelloMongoDB &lbrace; &NewLine; &NewLine; &commat;Autowired &NewLine; MongoOperations mongoOperations&semi; &NewLine; &NewLine; public void execute&lpar;&rpar; &lbrace; &NewLine; if &lpar;mongoOperations&period;collectionExists&lpar;"dojCollection"&rpar;&rpar; &lbrace; &NewLine; mongoOperations&period;dropCollection&lpar;"dojCollection"&rpar;&semi; &NewLine; &rcub; &NewLine; Employee employee3 &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee3&period;setEmpId&lpar;1001&rpar;&semi; &NewLine; employee3&period;setEmpName&lpar;"Dinesh Rajput"&rpar;&semi; &NewLine; employee3&period;setSalary&lpar;70000&rpar;&semi; &NewLine; employee3&period;setEmpAge&lpar;26&rpar;&semi; &NewLine; &NewLine; Employee employee4 &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee4&period;setEmpId&lpar;1002&rpar;&semi; &NewLine; employee4&period;setEmpName&lpar;"Adesh Rajput"&rpar;&semi; &NewLine; employee4&period;setSalary&lpar;30000&rpar;&semi; &NewLine; employee4&period;setEmpAge&lpar;23&rpar;&semi; &NewLine; &NewLine; Employee employee5 &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee5&period;setEmpId&lpar;1003&rpar;&semi; &NewLine; employee5&period;setEmpName&lpar;"Vinesh Rajput"&rpar;&semi; &NewLine; employee5&period;setSalary&lpar;32000&rpar;&semi; &NewLine; employee5&period;setEmpAge&lpar;23&rpar;&semi; &NewLine; &NewLine; Employee employee6 &equals; new Employee&lpar;&rpar;&semi; &NewLine; employee6&period;setEmpId&lpar;1004&rpar;&semi; &NewLine; employee6&period;setEmpName&lpar;"Sweety Rajput"&rpar;&semi; &NewLine; employee6&period;setSalary&lpar;50000&rpar;&semi; &NewLine; employee6&period;setEmpAge&lpar;22&rpar;&semi; &NewLine; &NewLine; List&lt&semi;Employee&gt&semi; empList &equals; new ArrayList&lt&semi;Employee&gt&semi;&lpar;&rpar;&semi; &NewLine; empList&period;add&lpar;employee3&rpar;&semi; &NewLine; empList&period;add&lpar;employee4&rpar;&semi; &NewLine; empList&period;add&lpar;employee5&rpar;&semi; &NewLine; empList&period;add&lpar;employee6&rpar;&semi; &NewLine; &NewLine; mongoOperations&period;insert&lpar;empList&comma; "dojCollection"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;CASE 1&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi; &NewLine; &sol;&sol; Case 1 &period;&period;&period; where empId &equals; 1001 &NewLine; &sol;&sol; find &NewLine; Employee employee1 &equals; mongoOperations&period;findOne&lpar;query&lpar;where&lpar;"empId"&rpar;&period;is&lpar;1001&rpar;&rpar;&comma; Employee&period;class&comma;"dojCollection"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;employee1&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;CASE 2&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi; &NewLine; &sol;&sol; Case 2 &period;&period;&period; where empAge &gt&semi;&equals; 23 and salary &equals; 70000 &NewLine; &sol;&sol; find &NewLine; List&lt&semi;Employee&gt&semi; employee2 &equals; mongoOperations&period;find&lpar;query&lpar;where&lpar;"salary"&rpar;&period;is&lpar;70000&rpar;&period;and&lpar;"empAge"&rpar;&period;gte&lpar;23&rpar;&rpar;&comma; Employee&period;class&comma;"dojCollection"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;employee2&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;CASE 3&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;"&rpar;&semi; &NewLine; &sol;&sol; Case 3 &period;&period;&period; where empId &lt&semi;&equals; 10002 and age &equals; 23 &NewLine; &sol;&sol; find &NewLine; List&lt&semi;Employee&gt&semi; employee &equals; mongoOperations&period;find&lpar;query&lpar;where&lpar;"empId"&rpar;&period;lte&lpar;10002&rpar;&period;and&lpar;"empAge"&rpar;&period;is&lpar;23&rpar;&rpar;&comma; Employee&period;class&comma;"dojCollection"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;employee&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>Step 4&colon; Running the Example<&sol;b><&sol;h3>&NewLine;<p>Following code shows how to run this example<&sol;p>&NewLine;<p><b>HelloMongoTestApp&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;mongo&period;main&semi; &NewLine; &NewLine;import org&period;springframework&period;context&period;ConfigurableApplicationContext&semi; &NewLine;import org&period;springframework&period;context&period;support&period;ClassPathXmlApplicationContext&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class HelloMongoTestApp &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; ConfigurableApplicationContext context &equals; new ClassPathXmlApplicationContext&lpar;"mongo-config&period;xml"&rpar;&semi; &NewLine; &NewLine; HelloMongoDB hello &equals; &lpar;HelloMongoDB&rpar; context&period;getBean&lpar;"helloMongoDB"&rpar;&semi; &NewLine; hello&period;execute&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar; "DONE&excl;" &rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>If everything is fine then run the above main application as Java Application we will get the following<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b>output&colon;<&sol;b><br &sol;>&NewLine;log4j&colon;WARN No appenders could be found for logger &lpar;org&period;springframework&period;context&period;support&period;ClassPathXmlApplicationContext&rpar;&period;<br &sol;>&NewLine;log4j&colon;WARN Please initialize the log4j system properly&period;<br &sol;>&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;CASE 1&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;<br &sol;>&NewLine;Employee &lbrack;age&equals;26&comma; empName&equals;Dinesh Rajput&comma; empId&equals;1001&comma; salary&equals;70000&rsqb;<br &sol;>&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;CASE 2&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;<br &sol;>&NewLine;&lbrack;Employee &lbrack;age&equals;26&comma; empName&equals;Dinesh Rajput&comma; empId&equals;1001&comma; salary&equals;70000&rsqb;&rsqb;<br &sol;>&NewLine;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;CASE 3&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;&ast;<br &sol;>&NewLine;&lbrack;Employee &lbrack;age&equals;23&comma; empName&equals;Adesh Rajput&comma; empId&equals;1002&comma; salary&equals;30000&rsqb;&comma; Employee &lbrack;age&equals;23&comma; empName&equals;Vinesh Rajput&comma; empId&equals;1003&comma; salary&equals;32000&rsqb;&rsqb;<br &sol;>&NewLine;DONE&excl;<&sol;div>&NewLine;<h3><b>Download SourceCode&plus;Libs<&sol;b><br &sol;>&NewLine;<a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;site&sol;dinesh9582486434&sol;my-forms&sol;MongoDBSpringQueryDemo&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener"><b>MongoDBSpringQueryDemo&period;zip<&sol;b><&sol;a><br &sol;>&NewLine;<b><&sol;b><&sol;h3>&NewLine;<p><b>References<&sol;b><br &sol;>&NewLine;You must read this <a href&equals;"http&colon;&sol;&sol;static&period;springsource&period;org&sol;spring-data&sol;data-document&sol;docs&sol;1&period;0&period;0&period;M2&sol;reference&sol;html&sol;&num;mongodb-template-query" target&equals;"&lowbar;blank" rel&equals;"noopener"><b>Spring’s mongodb query documentation<&sol;b><&sol;a>&comma; to study more overloaded methods and criteria examples&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;">                        <b>     &lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-mongodb-update-document&sol;">previous<&sol;a>&lt&semi;&lt&semi;             &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-mongodb&sol;">index  <&sol;a>&vert;&vert;         &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-mongodb-delete-document&sol;">next<&sol;a>&gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-mongodb-update-document&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-mongodb-delete-document&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '581'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago