42 | `findOne()` ⇒ First matching document(if filter given) or first element of document | Simple movie sample
43 |
44 | ```bash
45 | > use movieData
46 | switched to db movieData
47 | > cls
48 |
49 | // findOne() => First matching document
50 |
51 | > db.movies.findOne()
52 | {
53 | "_id" : ObjectId("5f15a22a9bfbc37d06f66616"),
54 | "id" : 1,
55 | "url" : "http://www.tvmaze.com/shows/1/under-the-dome",
56 | "name" : "Under the Dome",
57 | "type" : "Scripted",
58 | "language" : "English",
59 | "genres" : [
60 | "Drama",
61 | "Science-Fiction",
62 | "Thriller"
63 | ],
64 | "status" : "Ended",
65 | "runtime" : 60,
66 | "premiered" : "2013-06-24",
67 | "officialSite" : "http://www.cbs.com/shows/under-the-dome/",
68 | "schedule" : {
69 | "time" : "22:00",
70 | "days" : [
71 | "Thursday"
72 | ]
73 | },
74 | "rating" : {
75 | "average" : 6.5
76 | },
77 | "weight" : 91,
78 | "network" : {
79 | "id" : 2,
80 | "name" : "CBS",
81 | "country" : {
82 | "name" : "United States",
83 | "code" : "US",
84 | "timezone" : "America/New_York"
85 | }
86 | },
87 | "webChannel" : null,
88 | "externals" : {
89 | "tvrage" : 25988,
90 | "thetvdb" : 264492,
91 | "imdb" : "tt1553656"
92 | },
93 | "image" : {
94 | "medium" : "http://static.tvmaze.com/uploads/images/medium_portrait/0/1.jpg",
95 | "original" : "http://static.tvmaze.com/uploads/images/original_untouched/0/1.jpg"
96 | },
97 | "summary" : "Under the Dome is the story of a small town that is suddenly and inexplicably sealed off from the rest of the world by an enormous transparent dome. The town's inhabitants must deal with surviving the post-apocalyptic conditions while searching for answers about the dome, where it came from and if and when it will go away.
",
98 | "updated" : 1529612668,
99 | "_links" : {
100 | "self" : {
101 | "href" : "http://api.tvmaze.com/shows/1"
102 | },
103 | "previousepisode" : {
104 | "href" : "http://api.tvmaze.com/episodes/185054"
105 | }
106 | }
107 | }
108 | ```
109 |
110 |
111 |
112 |
113 |
114 |
115 | # Query Selectors & Projection
116 |
117 | ## Query Selectors
118 |
119 | [Comparison](Read/Comparison.md)
120 |
121 | [Logical](Read/Logical.md)
122 |
123 | [Element](Read/Element.md)
124 |
125 | [Evaluation Operators](Read/Evaluation_Operators.md)
126 |
127 | [Array](Read/Array.md)
128 |
129 | Comments
130 |
131 | Geospatial(special)
132 |
133 | ### Projections Operators
134 |
135 | 1. $
136 | 2. $elemMatch
137 | 3. $meta
138 | 4. $slice
139 |
140 | [Projection](Read/Projection.md)
141 |
142 | # Understanding Cursors
143 |
144 | When we use `find()` can get the all data like 100 million
145 |
146 | It can reduce if we include a query.
147 |
148 | Here cursors a pointer. cursor request batch to the server every time to get that data.
149 |
150 | If have a query that meets 1000 documents, but let's say you have a website where you only display 10 items, let's say 10 products you fetched at a time anyways, then there is no need to load all thousand results that matched your query right at the start. Instead, you would only fetch the first 10, display them on the screen and then go ahead and fetch the next 10, when the user navigated to the next page or anything like that. This is the idea behind a cursor.
151 |
152 | ```cpp
153 | > use movieData
154 | > db.movies.find().count()
155 | 240
156 | ```
157 |
158 | Basically it returns 20 elements
159 |
160 | ```cpp
161 | db.movies.find().pretty()
162 | // type it for more
163 | > it
164 | ```
165 |
166 | Get exactly one document, because next() gives the next document.
167 |
168 | `db.movies.find().next()` => giving this command will return the same document over and over as it works from scratch(from first) every time.
169 |
170 | ```cpp
171 | > db.movies.find().next()
172 | ```
173 |
174 | We can use JavaScript syntax in the mongo shell(We will get documents one by one as we stored them)
175 |
176 | ```bash
177 | > const dataCursor = db.movies.find()
178 | > dataCursor.next() => first element
179 | > dataCursor.next() => second element
180 | ```
181 |
182 | `printjson()` is a mongo shell function that helps to print something into shell it fetched all documents(so it will not return cursor)
183 |
184 | ```bash
185 | dataCursor.forEach(document => {printjson(document)})
186 | ```
187 |
188 | Check have any next value
189 |
190 | ```bash
191 | dataCursor.hasNext()
192 | ```
193 |
194 | Fetching data with sort(), One means ascending
195 |
196 | ```bash
197 | db.movies.find().sort({'rating.average': 1}).pretty()
198 | ```
199 |
200 | eMinus one mean descending
201 |
202 | ```bash
203 | db.movies.find().sort({'rating.average': -1}).pretty()e
204 | ```
205 |
206 | Sort with multiple documents, here first sort the `'rating.average'` and if `'rating.average'` have the same value into particular indexes and then runtime executes with descending if may exist.
207 |
208 | ```bash
209 | > db.movies.find().sort({'rating.average': 1, runtime: -1}).pretty()
210 | ```
211 |
212 | The cursor also works with sort()
213 |
214 | ```bash
215 | > db.movies.find().sort({'rating.average': 1, runtime: -1}).next()
216 | ```
217 |
218 | We can skip a certain amount of elements, it is effective in pagination, when we work with pagination on the second page we need data from the 11th element, we can skip the previous 10 elements.
219 |
220 | ```bash
221 | db.movies.find().sort({'rating.average': 1, runtime: -1}).skip(10).pretty()
222 | ```
223 |
224 | `skip()` is used to specify the number of documents to skip at the first time.
225 | We can still include `limit()` limit return the exact number of elements will be retrieved at a time.
226 |
227 | ```bash
228 | db.movies.find().sort({'rating.average': 1,runtime: -1}).skip(100).limit(10).pretty()
229 | ```
230 |
231 | Here order does not matter. Order check from right such(previous example) : `sort()` -> `skip()` -> `limit()` . what method we **write first**, it **executes first**.
--------------------------------------------------------------------------------
/MongoDB/Read/Array.md:
--------------------------------------------------------------------------------
1 | # Array
2 |
3 | `$elemMatch`
4 |
5 | `$size`
6 |
7 | `$all`
8 |
9 |