Skip to content

Commit c257f9c

Browse files
DOCSP-9567 crud query (#68)
* added CRUD > Specify a Query
1 parent 9063297 commit c257f9c

File tree

3 files changed

+311
-0
lines changed

3 files changed

+311
-0
lines changed

source/fundamentals/crud.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CRUD Operations
99

1010
/fundamentals/crud/read-operations
1111
/fundamentals/crud/write-operations
12+
/fundamentals/crud/query-document
1213

1314
CRUD (Create, Read, Update, Delete) operations enable you to work with
1415
data stored in MongoDB.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
===============
2+
Specify a Query
3+
===============
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
Most CRUD operations allow you to narrow the set of matched documents by
17+
specifying matching criteria in a **query document**. Query documents
18+
contain one or more query operators that apply to specific fields which
19+
determine which documents to include in the result set.
20+
21+
In this page, we cover the following query operators with
22+
examples on how to use them:
23+
24+
- :ref:`Comparison Operators <query-comparison>`
25+
- :ref:`Logical Operators <query-logical>`
26+
- :ref:`Array Operators <query-arrays>`
27+
- :ref:`Element Operators <query-elements>`
28+
- :ref:`Evaluation Operators <query-evaluation>`
29+
30+
The examples in this guide use the following documents in the
31+
``paint_purchases`` collection:
32+
33+
.. code-block:: json
34+
35+
{ "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] }
36+
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
37+
{ "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] }
38+
{ "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 }
39+
{ "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] }
40+
{ "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] }
41+
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }
42+
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }
43+
44+
.. _query-comparison:
45+
46+
Comparison Operators
47+
--------------------
48+
49+
Comparison operators query data based on comparisons with values in a
50+
collection. Common comparison operators include ``gt()`` for "greater
51+
than" comparisons, ``lte()`` for "less than or equal to" comparisons,
52+
and ``ne()`` for "not equal to " comparisons.
53+
54+
The following example uses the ``Filters.gt()`` method to match all
55+
documents where the value of ``qty`` is greater than ``7`` in the
56+
``paint_purchases`` collection:
57+
58+
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
59+
:language: java
60+
:dedent:
61+
:start-after: begin comparisonFilter
62+
:end-before: end comparisonFilter
63+
64+
The following shows the output of the query using the operator specified
65+
above:
66+
67+
.. code-block:: json
68+
:copyable: false
69+
70+
{ "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] }
71+
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
72+
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }
73+
74+
.. _query-logical:
75+
76+
Logical Operators
77+
-----------------
78+
79+
Logical operators query data using logic applied to the results of
80+
field-level operators. Common logical operators include ``and()`` where
81+
all operators must be true, and ``or()`` where at least one of the
82+
operators must be true.
83+
84+
The following example uses the ``Filters.and()`` method to match
85+
documents where the value of ``qty`` is less than or equal to ``5`` and
86+
the value of ``color`` is not ``"pink"`` in the ``paint_purchases``
87+
collection:
88+
89+
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
90+
:language: java
91+
:dedent:
92+
:start-after: begin logicalFilter
93+
:end-before: end logicalFilter
94+
95+
The following shows the output of the query using the operator specified
96+
above:
97+
98+
.. code-block:: json
99+
:copyable: false
100+
101+
{ "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] }
102+
{ "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] }
103+
104+
.. _query-arrays:
105+
106+
Array Operators
107+
---------------
108+
109+
Array operators query data based on the value or quantity of elements in
110+
an array field.
111+
112+
The following example uses the ``Filters.size()`` method to match
113+
documents where the size of ``vendor`` list is ``3`` in the
114+
``paint_purchases`` collection:
115+
116+
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
117+
:language: java
118+
:dedent:
119+
:start-after: begin arrayFilter
120+
:end-before: end arrayFilter
121+
122+
The following shows the output of the query using the operator specified
123+
above:
124+
125+
.. code-block:: json
126+
:copyable: false
127+
128+
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
129+
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }
130+
131+
.. _query-elements:
132+
133+
Element Operators
134+
-----------------
135+
136+
Element operators query data based on the presence or type of a field.
137+
138+
The following example uses the ``Filters.exists()`` method to match
139+
documents that have a ``rating`` in the ``paint_purchases`` collection:
140+
141+
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
142+
:language: java
143+
:dedent:
144+
:start-after: begin elementFilter
145+
:end-before: end elementFilter
146+
147+
The following shows the output of the query using the operator specified
148+
above:
149+
150+
.. code-block:: json
151+
:copyable: false
152+
153+
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
154+
{ "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 }
155+
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }
156+
157+
.. _query-evaluation:
158+
159+
Evaluation Operators
160+
--------------------
161+
162+
Evaluation operators query data on higher level logic, like regex
163+
and text searches.
164+
165+
The following example uses the ``Filters.regex()`` method to match
166+
documents that have a ``color`` ending with the letter ``"k"`` in the
167+
``paint_purchases`` collection:
168+
169+
.. literalinclude:: /includes/fundamentals/code-snippets/Query.java
170+
:language: java
171+
:dedent:
172+
:start-after: begin evaluationFilter
173+
:end-before: end evaluationFilter
174+
175+
The following shows the output of the query using the operator specified
176+
above:
177+
178+
.. code-block:: json
179+
:copyable: false
180+
181+
{ "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] }
182+
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }
183+
184+
See the following documentation for more information about the operators
185+
in this guide:
186+
187+
- :manual:`Query Operators </reference/operator/query/>`
188+
- :manual:`Comparison Operators </reference/operator/query-comparison/>`
189+
- :manual:`Logical Operators </reference/operator/query-logical/>`
190+
- :manual:`Array Operators </reference/operator/query-array/>`
191+
- :manual:`Element Operators </reference/operator/query-element/>`
192+
- :manual:`Evaluation Operators </reference/operator/query-evaluation/>`
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package docs;
2+
3+
import com.mongodb.client.MongoClient;
4+
import com.mongodb.client.MongoClients;
5+
import com.mongodb.client.MongoCollection;
6+
import com.mongodb.client.MongoDatabase;
7+
8+
import org.bson.Document;
9+
import org.bson.conversions.Bson;
10+
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
15+
import com.mongodb.client.model.Filters;
16+
17+
public class Query {
18+
19+
private final MongoCollection<Document> collection;
20+
private final MongoClient mongoClient;
21+
private final MongoDatabase database;
22+
23+
private Query() {
24+
final String uri = System.getenv("DRIVER_REF_URI");
25+
26+
mongoClient = MongoClients.create(uri);
27+
database = mongoClient.getDatabase("crudOps");
28+
collection = database.getCollection("query");
29+
}
30+
31+
public static void main(String [] args){
32+
Query query = new Query();
33+
// query.preview();
34+
// query.setupPaintCollection();
35+
36+
System.out.println("Comparison Filter:");
37+
query.comparisonFilter();
38+
39+
System.out.println("Logical Filter:");
40+
query.logicalFilter();
41+
42+
System.out.println("Element Filter:");
43+
query.elementFilter();
44+
45+
System.out.println("Evaluation Filter:");
46+
query.evaluationFilter();
47+
48+
System.out.println("Array Filter:");
49+
query.arrayFilter();
50+
}
51+
52+
private void comparisonFilter(){
53+
// begin comparisonFilter
54+
Bson filter = Filters.gt("qty", 7);
55+
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
56+
// end comparisonFilter
57+
}
58+
59+
private void logicalFilter(){
60+
// begin logicalFilter
61+
Bson filter = Filters.and(Filters.lte("qty", 5), Filters.ne("color", "pink"));
62+
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
63+
// end logicalFilter
64+
}
65+
66+
private void elementFilter(){
67+
// begin elementFilter
68+
Bson filter = Filters.exists("rating");
69+
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
70+
// end elementFilter
71+
}
72+
73+
private void evaluationFilter(){
74+
// begin evaluationFilter
75+
Bson filter = Filters.regex("color", "k$");
76+
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
77+
// end evaluationFilter
78+
}
79+
80+
private void arrayFilter(){
81+
// begin arrayFilter
82+
Bson filter = Filters.size("vendor", 3);
83+
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
84+
// end arrayFilter
85+
}
86+
87+
private void preview(){
88+
// Bson filter = Filters.empty();
89+
// collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
90+
collection.drop();
91+
}
92+
93+
private void setupPaintCollection() {
94+
95+
List<Document> querydata = new ArrayList<>();
96+
97+
Document p1 = new Document("_id", 1).append("color", "red").append("qty", 9).append("vendor", Arrays.asList("A", "E"));
98+
Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 8).append("vendor", Arrays.asList("B", "D", "F")).append("rating", 5);
99+
Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 5).append("vendor", Arrays.asList("A", "E"));
100+
Document p4 = new Document("_id", 4).append("color", "white").append("qty", 6).append("vendor", Arrays.asList("D")).append("rating", 9);
101+
Document p5 = new Document("_id", 5).append("color", "yellow").append("qty", 4).append("vendor", Arrays.asList("A", "B"));
102+
Document p6 = new Document("_id", 6).append("color", "pink").append("qty", 3).append("vendor", Arrays.asList("C"));
103+
Document p7 = new Document("_id", 7).append("color", "green").append("qty", 8).append("vendor", Arrays.asList("C", "E")).append("rating", 7);
104+
Document p8 = new Document("_id", 8).append("color", "black").append("qty", 7).append("vendor", Arrays.asList("A", "C", "D"));
105+
106+
querydata.add(p1);
107+
querydata.add(p2);
108+
querydata.add(p3);
109+
querydata.add(p4);
110+
querydata.add(p5);
111+
querydata.add(p6);
112+
querydata.add(p7);
113+
querydata.add(p8);
114+
115+
collection.insertMany(querydata);
116+
}
117+
118+
}

0 commit comments

Comments
 (0)