Skip to content
Browse files

added method to create indices. geo test case.

  • Loading branch information...
1 parent 6b580ef commit 686e3100dbde3879d08b238f4905eb183b3afd4e Tim Burks committed Jun 21, 2010
Showing with 48 additions and 0 deletions.
  1. +39 −0 geo.nu
  2. +9 −0 objc/NuMongoDB.m
View
39 geo.nu
@@ -0,0 +1,39 @@
+(load "NuMongoDB")
+
+(set collection "geo.places")
+
+(set mongo (NuMongoDB new))
+
+(puts "connecting")
+(mongo connectWithOptions:(dict host:"127.0.0.1"))
+(puts "connected")
+
+(NuMath random)
+
+(if NO
+ (puts "building")
+ ;; rebuild the place database
+ (mongo dropCollection:"places" inDatabase:"geo")
+ (set N 1000)
+ (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 insert:place intoCollection:collection)))))
+ (mongo ensureCollection:"geo.places" hasIndex:(dict location:"2d") withOptions:0))
+
+;; search the place database
+(puts "querying")
+(set cursor (mongo find:(dict location:(dict $near:(dict latitude:110 longitude:80))) inCollection:"geo.places"))
+(set i 0)
+(while (and (cursor next) (< i 10))
+ (set bson (cursor currentBSON))
+ (set object (bson dictionaryValue))
+ (puts (object description))
+ (set i (+ i 1)))
View
9 objc/NuMongoDB.m
@@ -435,6 +435,15 @@ - (BOOL) dropCollection:(NSString *) collection inDatabase:(NSString *) database
NULL);
}
+- (BOOL) ensureCollection:(NSString *) collection hasIndex:(NSObject *) key withOptions:(int) options {
+ bson output;
+ return mongo_create_index(conn,
+ [collection cStringUsingEncoding:NSUTF8StringEncoding],
+ bson_for_object(key),
+ options,
+ &output);
+}
+
- (void) close
{
mongo_destroy(conn );

0 comments on commit 686e310

Please sign in to comment.
Something went wrong with that request. Please try again.