forked from timburks/NuMongoDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.nu
43 lines (36 loc) · 1.39 KB
/
geo.nu
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
(load "NuMongoDB")
(set collection "geo.places")
(set mongo (NuMongoDB new))
(puts "connecting")
(mongo connectWithOptions:(dict host:"127.0.0.1"))
(puts "connected")
(set count (mongo countWithCondition:nil inCollection:"places" inDatabase:"geo"))
(puts count)
(if NO
(puts "building")
;; rebuild the place database
(mongo dropCollection:"places" inDatabase:"geo")
(set N 200)
(N times:
(do (i)
;(set latitude (* i (/ 180 N)))
(N times:
(do (j)
;(set longitude (* j (/ 180 N)))
(set latitude (/ (% (NuMath random) 180000) 1000))
(set longitude (/ (% (NuMath random) 180000) 1000))
(set place (dict name:(+ "location-" i "-" j)
location:(dict latitude:latitude longitude:longitude)))
(mongo insertObject:place intoCollection:collection)))))
(mongo ensureCollection:"geo.places" hasIndex:(dict location:"2d") withOptions:0))
;; search the place database
(puts "querying")
(100 times:
(do (i)
(set cursor (mongo find:(dict location:(dict $near:(dict latitude:70 longitude:80))) inCollection:"geo.places"))
(set i 0)
(while (and (cursor next) (< i 10))
(set object (cursor currentObject))
(puts (object description))
(set i (+ i 1)))
))