/
finding-things-in-a-mongo-database-.html
107 lines (88 loc) · 4.11 KB
/
finding-things-in-a-mongo-database-.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!---
;;
;; This softeware is Copyright (c) 2009 A.F. Haffmans
;;
;; This file is part of cl-bliky.
;;
;; cl-bliky is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; cl-bliky is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with cl-bliky. If not, see <http://www.gnu.org/licenses/>.
;;
;;
--->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Mohegan SkunkWorks </title>
<link rel="stylesheet" href="/style.css" type="text/css"/>
</head>
<body>
<div id="blog-title">
<a href="/"> <h1> Mohegan SkunkWorks </h1> </a>
</div>
<div id="mainClm">
<div id="post">
<div id="post-type" style="visibility:hidden">
<p>"post"</p>
</div>
<div id="post-timestamp" >
<p>Sat, 26 Jun 2010 20:00:53 EST </p>
</div>
<div id="post-header">
<h2> finding things in a mongo database </h2>
</div>
<div id="post-intro">
<p>The generic method <em>dbfind</em> provides the basic query interface in cl-mongo. It's defined as:</p>
<pre><code>(defgeneric db.find (collection kv &key) )
</code></pre>
<p><em>db.find</em> returns documents in the collection which satisfy the query pattern
specified by the bson document kv. If you use the keyword <em>:all</em> db.find will return all documents. If you're looking for all documents with specific (key value) pair you'd specify that those as the query pattern :</p>
<pre><code> (db.find "foo" (kv (kv "k" 3) (kv "l" 4)))
</code></pre>
</div>
<div id="post-body">
<p><em>db.find</em> returns a header and a list of documents. A single call to db.find returns at most a 100
documents or whatever mongodb has set as the limit to return in a single call. To get more documents
you'd need to query the database with the iterator object returned by <em>db.find</em>.
The convenience function <em>iter</em> does exactly that. So, in order to return <em>all</em> documents
in collection "foo" you'd call :</p>
<pre><code> (pp (iter (db.find "foo" :all)))
</code></pre>
<p><em>pp</em> is a pretty printer provided by <em>cl_mongo</em>. Alternatively, you can use the <em>docs</em> function to
convert the results of the query in a list of documents for further processing. Both <em>pp</em> and <em>docs</em>
will properly clean up the iterator object. </p>
<p><em>db.find</em> accepts a number of keywords. The keyword <em>:limit</em> defines the maximum number
of documents returned, with 1 as it's default. However when the query pattern is <em>:all</em> it will in fact
be 0, and hence <em>db.find</em> will return the maximum number of documents allowable by mongodb.
In all other cases, since the default value of <em>:limit</em> is 1, db.find
is the equivalent of <em>findOne</em> in the mongo documentation.</p>
<p>Unsurprisingly, you can specify the number of documents to skip in this query with <em>:skip</em>.
It's default is 0.</p>
<p>The <em>:selector</em> keyword allows you to select which keys to return. For example,
if you just want to return the <em>"_id"</em> field for the objects in foo, you'd specify :</p>
<pre><code> (pp (iter (db.find "foo" :all :selector "_id" )))
</code></pre>
<p>The keyword <em>:mongo</em> allows you to specify a connection other than the default connection.</p>
<p>The keyword <em>:options</em> is used to specify query options and will be covered elsewhere.</p>
</div>
<div id="post-timestamp-id" style="visibility:hidden">
<p>3486585653 </p>
</div>
</div>
<div id="footer">
<p>
Powered by <a href="http://www.github.com/fons/cl-bliky"> cl-bliky </a>
</p>
</div>
</div><!-- end of main area -->
</body>
</html>