Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

dictionaries are copied before _id's are added. new sample code.

insertion was throwing exceptions for non-mutable dictionaries.
also there is new Mac OS sample code illustrating geo lookup.
  • Loading branch information...
commit 3b1c40c332863617dee7bd98e77b6e6224c895bf 1 parent 42e9cbd
@timburks authored
View
5 .gitignore
@@ -0,0 +1,5 @@
+*.mode1v3
+*.pbxuser
+build
+*.framework
+
View
74 MacOS/MongoPlacesTest/MongoPlacesTest.m
@@ -0,0 +1,74 @@
+#import <Foundation/Foundation.h>
+#import <NuMongoDB/NuMongoDB.h>
+
+static BOOL always_rebuild = YES;
+
+int main (int argc, const char * argv[]) {
+ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
+ NuMongoDB *mongo = [NuMongoDB new];
+
+ // connect to a local database
+ NSDictionary *HOSTINFO = [NSDictionary dictionary];
+ [mongo connectWithOptions:HOSTINFO];
+
+ int count = [mongo countWithCondition:nil
+ inCollection:@"places"
+ inDatabase:@"sample"];
+
+ NSLog(@"number of places in database: %d", count);
+
+ if ((count == 0) || always_rebuild) {
+ NSLog(@"rebuilding");
+ // rebuild the place database
+ [mongo dropCollection:@"places" inDatabase:@"sample"];
+ const int N = 500;
+ for (int i = 0; i < N; i++) {
+ for (int j = 0; j < N; j++) {
+ float latitude = (random() % 180000) / 1000.0;
+ float longitude = (random() % 180000) / 1000.0;
+ NSMutableDictionary *place = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ [NSString stringWithFormat:@"location-%d-%d", i, j], @"name",
+ [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSNumber numberWithFloat:latitude], @"latitude",
+ [NSNumber numberWithFloat:longitude], @"longitude",
+ nil], @"location",
+ nil];
+ [mongo insertObject:place intoCollection:@"sample.places"];
+ }
+ }
+ count = [mongo countWithCondition:nil
+ inCollection:@"places"
+ inDatabase:@"sample"];
+
+ NSLog(@"number of places in database: %d", count);
+ }
+
+ [mongo ensureCollection:@"sample.places"
+ hasIndex:[NSDictionary dictionaryWithObjectsAndKeys:
+ @"2d", @"location",
+ nil]
+ withOptions:0];
+
+ // search the place database
+ NSLog(@"querying");
+ NuMongoDBCursor *cursor = [mongo find:
+ [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSNumber numberWithFloat:90.0], @"latitude",
+ [NSNumber numberWithFloat:90.0], @"longitude",
+ nil], @"$near",
+ nil], @"location",
+ nil]
+ inCollection:@"sample.places"];
+ int i = 0;
+ while ([cursor next] && (i < 3)) {
+ id object = [cursor currentObject];
+ NSLog(@"%@", [object description]);
+ i++;
+ }
+
+ [pool drain];
+ return 0;
+}
View
232 MacOS/MongoPlacesTest/MongoPlacesTest.xcodeproj/project.pbxproj
@@ -0,0 +1,232 @@
+// !$*UTF8*$!
+{
+ archiveVersion = 1;
+ classes = {
+ };
+ objectVersion = 45;
+ objects = {
+
+/* Begin PBXBuildFile section */
+ 221C3337128120540040CE11 /* NuMongoDB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 221C3336128120540040CE11 /* NuMongoDB.framework */; };
+ 8DD76F9A0486AA7600D96B5E /* MongoPlacesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* MongoPlacesTest.m */; settings = {ATTRIBUTES = (); }; };
+ 8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
+ 8DD76F9F0486AA7600D96B5E /* MongoPlacesTest.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859EA3029092ED04C91782 /* MongoPlacesTest.1 */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXCopyFilesBuildPhase section */
+ 8DD76F9E0486AA7600D96B5E /* CopyFiles */ = {
+ isa = PBXCopyFilesBuildPhase;
+ buildActionMask = 8;
+ dstPath = /usr/share/man/man1/;
+ dstSubfolderSpec = 0;
+ files = (
+ 8DD76F9F0486AA7600D96B5E /* MongoPlacesTest.1 in CopyFiles */,
+ );
+ runOnlyForDeploymentPostprocessing = 1;
+ };
+/* End PBXCopyFilesBuildPhase section */
+
+/* Begin PBXFileReference section */
+ 08FB7796FE84155DC02AAC07 /* MongoPlacesTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MongoPlacesTest.m; sourceTree = "<group>"; };
+ 08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
+ 221C3336128120540040CE11 /* NuMongoDB.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NuMongoDB.framework; path = Library/Frameworks/NuMongoDB.framework; sourceTree = SDKROOT; };
+ 32A70AAB03705E1F00C91783 /* MongoPlacesTest_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MongoPlacesTest_Prefix.pch; sourceTree = "<group>"; };
+ 8DD76FA10486AA7600D96B5E /* MongoPlacesTest */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = MongoPlacesTest; sourceTree = BUILT_PRODUCTS_DIR; };
+ C6859EA3029092ED04C91782 /* MongoPlacesTest.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = MongoPlacesTest.1; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+ 8DD76F9B0486AA7600D96B5E /* Frameworks */ = {
+ isa = PBXFrameworksBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */,
+ 221C3337128120540040CE11 /* NuMongoDB.framework in Frameworks */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+ 08FB7794FE84155DC02AAC07 /* MongoPlacesTest */ = {
+ isa = PBXGroup;
+ children = (
+ 08FB7795FE84155DC02AAC07 /* Source */,
+ C6859EA2029092E104C91782 /* Documentation */,
+ 08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */,
+ 1AB674ADFE9D54B511CA2CBB /* Products */,
+ );
+ name = MongoPlacesTest;
+ sourceTree = "<group>";
+ };
+ 08FB7795FE84155DC02AAC07 /* Source */ = {
+ isa = PBXGroup;
+ children = (
+ 32A70AAB03705E1F00C91783 /* MongoPlacesTest_Prefix.pch */,
+ 08FB7796FE84155DC02AAC07 /* MongoPlacesTest.m */,
+ );
+ name = Source;
+ sourceTree = "<group>";
+ };
+ 08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */ = {
+ isa = PBXGroup;
+ children = (
+ 08FB779EFE84155DC02AAC07 /* Foundation.framework */,
+ 221C3336128120540040CE11 /* NuMongoDB.framework */,
+ );
+ name = "External Frameworks and Libraries";
+ sourceTree = "<group>";
+ };
+ 1AB674ADFE9D54B511CA2CBB /* Products */ = {
+ isa = PBXGroup;
+ children = (
+ 8DD76FA10486AA7600D96B5E /* MongoPlacesTest */,
+ );
+ name = Products;
+ sourceTree = "<group>";
+ };
+ C6859EA2029092E104C91782 /* Documentation */ = {
+ isa = PBXGroup;
+ children = (
+ C6859EA3029092ED04C91782 /* MongoPlacesTest.1 */,
+ );
+ name = Documentation;
+ sourceTree = "<group>";
+ };
+/* End PBXGroup section */
+
+/* Begin PBXNativeTarget section */
+ 8DD76F960486AA7600D96B5E /* MongoPlacesTest */ = {
+ isa = PBXNativeTarget;
+ buildConfigurationList = 1DEB927408733DD40010E9CD /* Build configuration list for PBXNativeTarget "MongoPlacesTest" */;
+ buildPhases = (
+ 8DD76F990486AA7600D96B5E /* Sources */,
+ 8DD76F9B0486AA7600D96B5E /* Frameworks */,
+ 8DD76F9E0486AA7600D96B5E /* CopyFiles */,
+ );
+ buildRules = (
+ );
+ dependencies = (
+ );
+ name = MongoPlacesTest;
+ productInstallPath = "$(HOME)/bin";
+ productName = MongoPlacesTest;
+ productReference = 8DD76FA10486AA7600D96B5E /* MongoPlacesTest */;
+ productType = "com.apple.product-type.tool";
+ };
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+ 08FB7793FE84155DC02AAC07 /* Project object */ = {
+ isa = PBXProject;
+ buildConfigurationList = 1DEB927808733DD40010E9CD /* Build configuration list for PBXProject "MongoPlacesTest" */;
+ compatibilityVersion = "Xcode 3.1";
+ developmentRegion = English;
+ hasScannedForEncodings = 1;
+ knownRegions = (
+ English,
+ Japanese,
+ French,
+ German,
+ );
+ mainGroup = 08FB7794FE84155DC02AAC07 /* MongoPlacesTest */;
+ projectDirPath = "";
+ projectRoot = "";
+ targets = (
+ 8DD76F960486AA7600D96B5E /* MongoPlacesTest */,
+ );
+ };
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+ 8DD76F990486AA7600D96B5E /* Sources */ = {
+ isa = PBXSourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 8DD76F9A0486AA7600D96B5E /* MongoPlacesTest.m in Sources */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+ 1DEB927508733DD40010E9CD /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ALWAYS_SEARCH_USER_PATHS = NO;
+ COPY_PHASE_STRIP = NO;
+ GCC_DYNAMIC_NO_PIC = NO;
+ GCC_ENABLE_FIX_AND_CONTINUE = YES;
+ GCC_MODEL_TUNING = G5;
+ GCC_OPTIMIZATION_LEVEL = 0;
+ GCC_PRECOMPILE_PREFIX_HEADER = YES;
+ GCC_PREFIX_HEADER = MongoPlacesTest_Prefix.pch;
+ INSTALL_PATH = /usr/local/bin;
+ PRODUCT_NAME = MongoPlacesTest;
+ };
+ name = Debug;
+ };
+ 1DEB927608733DD40010E9CD /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ALWAYS_SEARCH_USER_PATHS = NO;
+ DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+ GCC_MODEL_TUNING = G5;
+ GCC_PRECOMPILE_PREFIX_HEADER = YES;
+ GCC_PREFIX_HEADER = MongoPlacesTest_Prefix.pch;
+ INSTALL_PATH = /usr/local/bin;
+ PRODUCT_NAME = MongoPlacesTest;
+ };
+ name = Release;
+ };
+ 1DEB927908733DD40010E9CD /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+ GCC_C_LANGUAGE_STANDARD = gnu99;
+ GCC_OPTIMIZATION_LEVEL = 0;
+ GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNUSED_VARIABLE = YES;
+ ONLY_ACTIVE_ARCH = YES;
+ PREBINDING = NO;
+ SDKROOT = macosx10.6;
+ };
+ name = Debug;
+ };
+ 1DEB927A08733DD40010E9CD /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+ GCC_C_LANGUAGE_STANDARD = gnu99;
+ GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNUSED_VARIABLE = YES;
+ PREBINDING = NO;
+ SDKROOT = macosx10.6;
+ };
+ name = Release;
+ };
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+ 1DEB927408733DD40010E9CD /* Build configuration list for PBXNativeTarget "MongoPlacesTest" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ 1DEB927508733DD40010E9CD /* Debug */,
+ 1DEB927608733DD40010E9CD /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+ 1DEB927808733DD40010E9CD /* Build configuration list for PBXProject "MongoPlacesTest" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ 1DEB927908733DD40010E9CD /* Debug */,
+ 1DEB927A08733DD40010E9CD /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+/* End XCConfigurationList section */
+ };
+ rootObject = 08FB7793FE84155DC02AAC07 /* Project object */;
+}
View
7 MacOS/MongoPlacesTest/MongoPlacesTest_Prefix.pch
@@ -0,0 +1,7 @@
+//
+// Prefix header for all source files of the 'MongoPlacesTest' target in the 'MongoPlacesTest' project.
+//
+
+#ifdef __OBJC__
+ #import <Foundation/Foundation.h>
+#endif
View
7 MacOS/NuMongoDB/NuMongoDB.xcodeproj/project.pbxproj
@@ -239,7 +239,14 @@
isa = PBXProject;
buildConfigurationList = 1DEB91B108733DA50010E9CD /* Build configuration list for PBXProject "NuMongoDB" */;
compatibilityVersion = "Xcode 3.1";
+ developmentRegion = English;
hasScannedForEncodings = 1;
+ knownRegions = (
+ English,
+ Japanese,
+ French,
+ German,
+ );
mainGroup = 0867D691FE84028FC02AAC07 /* NuMongoDB */;
productRefGroup = 034768DFFF38A50411DB9C8B /* Products */;
projectDirPath = "";
View
1  objc/NuMongoDB.m
@@ -148,6 +148,7 @@ - (NSMutableDictionary *) findOne:(id) query inCollection:(NSString *) collectio
- (id) insertObject:(id) insert intoCollection:(NSString *) collection
{
if (![insert objectForKey:@"_id"]) {
+ insert = [[insert mutableCopy] autorelease];
[insert setObject:[NuBSONObjectID objectID] forKey:@"_id"];
}
bson *b = bson_for_object(insert);
Please sign in to comment.
Something went wrong with that request. Please try again.